Educational Codeforces Round 16 -- C - Magic Odd Square (找规律)

来源:互联网 发布:金山快快软件管家 编辑:程序博客网 时间:2024/05/15 17:20

大体题意:

给你一个奇数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;}

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 43 5 76 9 8


0 0
原创粉丝点击