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
代码:
#include<iostream>
using namespace std;
int map[50][50];
int main()
{
int n;
cin>>n;
int num=1;
int x=1;
int y=n/2+1;
map[1][y]=1;
while(num<=n*n)
{
if(x==1&&y==n) map[++x][y]=++num;
if(x==1) x=n+1;
if(y==n) y=0;
if(map[x-1][y+1]) map[++x][y]=++num;
else map[--x][++y]=++num;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<n;j++)
{
cout<<map[i][j]<<" ";
}
cout<<map[i][n]<<endl;
}
return 0;
}