Tamflexの貯蔵庫

やる気のない備忘録

aoj 0078

魔方陣 | Aizu Online Judge
やるだけ

int main()
{
  int n;
  while(cin>>n,n)
  {
    vector<vector<int> > T(n,vector<int>(n,0));
    int k=1, x=n/2+1, y=n/2;
    while(k<=n*n)
    {
      while(T[x][y]!=0)
      {
        x = (x+1)%n;
        y = (y-1)%n;
        y = y<0? y+n : y;
      }
      T[x][y] = k;
      x = (x+1)%n;
      y = (y+1)%n;
      k++;
    }
    REP(i,n){REP(j,n) printf("%4d",T[i][j]); printf("\n");}
  }
  return 0;
}