###### Educational Codeforces Round 16 C 题 Magic Odd Square

C. Magic Odd Square
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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-n*n组成的矩阵中每个数都不相同且每行每列和两个对角线上每个元素的和都为奇数。

此题有两个需要注意的地方：

1：1这个数放在第一行最中间一列。

2：从1-n*n,每个数放在前一个数的右上角，即行加1，列减1（特判：上一个数在第一行第n列的时候，这个数应该在第二行第n列）。如果越界，即上个数行是1，列不为n，这个数行为n，列加1；上个数列为n，行不为1，这个数列为1，行加1.

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 500
using namespace std;
int ans[N][N];
int main()
{
int n,m,i,j,k,x,y,tx,ty;
scanf("%d",&n);
m=n*n;
x=1,y=n/2+1;
ans[x][y]=1;
for(i=2; i<=m; i++)
{
if(x==1&&y!=n)
{
tx=n,ty=y+1;

}
else if(y==n&&x!=1)
{
tx=x-1,ty=1;
}
else if(x==1&&y==n)
{
tx=x+1,ty=y;
}
else
{
if(ans[x-1][y+1])
tx=x+1,ty=y;
else
tx=x-1,ty=y+1;
}
ans[tx][ty]=i;
x=tx,y=ty;
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
printf("%d ",ans[i][j]);
}
puts("");
}
return 0;
}

#### 【解题报告】 Educational Codeforces Round 19

2017-04-16 17:16:52

#### 解题报告：Educational Codeforces Round 24 D,E,F

2017-07-01 14:07:43

#### Educational Codeforces Round #41 (D&E)

2018-04-05 10:38:15

#### Codeforces 710 C. Magic Odd Square（构造）——Educational Codeforces Round 16

2016-08-24 13:34:18

#### Educational Codeforces Round 36 (Rated for Div. 2) 题解

2018-01-13 23:16:24

#### Educational Codeforces Round 42 E 贪心

2018-04-11 11:57:41

#### Educational Codeforces Round 36 题解

2018-01-14 00:40:51

#### Educational Codeforces Round 42 (Rated for Div. 2) D. Merge Equals

2018-04-12 12:39:29

#### Educational Codeforces Round 36

2018-01-16 12:45:18

#### Codeforces Round #392 (Div. 2)E. Broken Tree

2017-01-20 19:16:32

## 不良信息举报

Educational Codeforces Round 16 C 题 Magic Odd Square