Magic Odd Square
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.
Input
The only line contains odd integer n (1 ≤ n ≤ 49).
Output
Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.
Examples
Input
1
Output
1
Input
3
Output
2 1 4
3 5 7
6 9 8
题意:给定一个n,使用1-nn的数字每个各一次,输出一个nn的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数。
方法:跟网上学的n阶奇幻方的做法
http://blog.csdn.net/fengchaokobe/article/details/7437767
#include<iostream>
using namespace std;
int a[55][55];
int main()
{
int n;
scanf("%d",&n);
int k=1;
int x=1,y=n/2+1;
a[x][y]=1;
int num=2;
while(num<=n*n)
{
if(x==1 && y==n)
a[++x][y]=num++;
if(x==1)
x=n+1;
if(y==n)
y=0;
if(a[x-1][y+1]==0)
a[--x][++y]=num++;
else
a[++x][y]=num++;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j!=1)
printf(" ");
printf("%d",a[i][j]);
}
cout<<endl;
}
return 0;
}