题目链接 : 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的 矩阵中 使得每行每列主对角线的和为奇数
解题思路:
附本题代码
—————————.
#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;
}