[原创]codeforces 710C Magic Odd Square 【杂类】

2016-08-23 21:02:49 Tabris_ 阅读数:367


博客爬取于2020-06-14 22:43:43
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/52294422


题目链接 : http://codeforces.com/problemset/problem/710/C
-----------------------------------.
C. Magic Odd Square
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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^2 填到N*N的 矩阵中 使得每行每列主对角线的和为奇数

解题思路:

附本题代码
---------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# include<stdio.h>
int main()
{
int a[100][100],x;/*初始化*/
int i,j,m,n,temp;
printf("输出魔方矩阵n=");
scanf("%d",&x);
while(x)
{
if(!(x%2))
{
printf("你输入了偶数,很遗憾本程序将退出");
return 0;
}

else
{
for(i=0; i<x; i++)
for(j=0; j<x; j++)
a[i][j]=0;
i=0;
j=x/2;
a[i][j]=1;
for(temp=2; temp<=x*x; temp++)
{
m=i;
n=j;
i--;
j++;
if(i<0) i=x-1;
if(j>=x) j=0;
if(a[i][j]!=0)
{
i=m+1;
j=n;
}

a[i][j]=temp;
}
}
for(i=0; i<x; i++)
{
for(j=0; j<x; j++)
printf("%4d",a[i][j]);
printf("\n");
}
printf("输出魔方矩阵n=");
scanf("%d",&x);
}
return 0;
}