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*N的方格里面填数1~N*N,横列斜和为奇数
哎,艰辛啊。。。
#include <bits/stdc++.h>
using namespace std;
#define Maxn 55
int MAP[Maxn][Maxn];
bool visit[Maxn][Maxn];
int main(){
int N;
cin >> N;
memset(visit,false,sizeof(visit));
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if( i >= (j- N/2) && i <= (j + N/2) && i + j >= ((N+3)/2) && i + j <= (N+(N+1)/2) ){
visit[i][j] = true;
}else{
visit[i][j] = false;
}
}
}
int jishu = 1;
int oushu = 2;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(visit[i][j]){
cout << jishu << " ";
jishu += 2;
}else{
cout << oushu << " ";
oushu += 2;
}
}
cout << endl;
}
}