大体题意:
给你一个奇数n,求出一个n*n矩阵,使得n*n矩阵 每一行,每一列,和主对角线上的数字之和都是奇数!
思路:
这个题出的很晚了,刚开始一直在推n 是偶数怎么办,没有看到n 是奇数= =!
又没看到要求对角线上也是奇数,交了发Wa!没想到最后还能改出来!
其实这个题目分析下样例就可以看出规律了!
他每一行奇数的个数是 1 , 3, 5 ,7 ,,n ,,7 , 5 , 3, 1.
把这些奇数放在中间,偶数从2开始填,奇数从1开始填就可以了!
#include<bits/stdc++.h>
using namespace std;
const int maxn = 300000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-10;
const double pi = acos(-1.0);
typedef long long ll;
int a[50][50];
int vis[2500];
int hang[50];
int main(){
int n;
scanf("%d",&n);
if (n == 1){
printf("1\n");
return 0;
}
int cnt = 1;
for (int i = 1; i <= (n+1)/2; ++i){
hang[cnt++] = i*2-1;
}
for (int i = n/2; i >= 1; --i){
hang[cnt++] = i*2-1;
}
--cnt;
int cur = 0;
int curji = 1,curou = 2;
for (;;){
if (curji >= n && curou >= n-1)break;
for (int i = 1; i <= n; ++i){
int j;
for (j = 1; j<= (n-hang[i])/2; j++ )a[i][j] = curou,curou += 2;
int curr = j-1;
for (; j <= curr + hang[i]; ++j)a[i][j]=curji,curji+=2;
for (;j <= n; ++j)a[i][j] = curou,curou+=2;
}
}
for (int i = 1; i<= n; ++i){
for (int j = 1; j <= n; ++j){
if (j > 1)printf(" ");
printf("%d",a[i][j]);
}
puts("");
}
return 0;
}