Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16

来源:互联网 发布:飞思卡尔单片机与pic 编辑:程序博客网 时间:2024/05/29 16:05

传送门

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(1n49).

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

题目大意:

给你一个数 n ,让你构造一个 nn 的矩阵,这 nn 的矩阵是由 1n2 这些数组

成的,构成的这个矩阵需要满足一个条件:

每一行 每一列 以及主对角线上的元素和必须是奇数。

让你输出这个矩阵,(输入数据保证 n 是奇数)。

解题思路:
举一些例子:(其中 1 代表的是奇数,0 代表的是偶数)
n=1 的时候:

矩阵是:

1

n=3 的时候:

矩阵是:

010111010

n=5 的时候:

矩阵式:

0010001110111110111000100

通过这些例子我们可以发现一些规律,就是按照对称的摆这些奇数就行了,找出这个规

律来了就行了。

My Code

/**2016 - 08 - 24 下午Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <set>using namespace std;typedef long long LL;typedef unsigned long long ULL;const int INF = 1e9+5;const int MAXN = 1e2+5;const int MOD = 1e9+7;const double eps = 1e-7;const double PI = acos(-1);using namespace std;LL Scan_LL()///输入外挂{    LL res=0,ch,flag=0;    if((ch=getchar())=='-')        flag=1;    else if(ch>='0'&&ch<='9')        res=ch-'0';    while((ch=getchar())>='0'&&ch<='9')        res=res*10+ch-'0';    return flag?-res:res;}int Scan_Int()///输入外挂{    int res=0,ch,flag=0;    if((ch=getchar())=='-')        flag=1;    else if(ch>='0'&&ch<='9')        res=ch-'0';    while((ch=getchar())>='0'&&ch<='9')        res=res*10+ch-'0';    return flag?-res:res;}void Out(LL a)///输出外挂{    if(a>9)        Out(a/10);    putchar(a%10+'0');}int a[MAXN][MAXN];void Init(int n){    memset(a, 0, sizeof(a));    for(int i=1; i<=n/2+1; i++)        for(int j=n/2+2-i; j<=n/2+i; j++)            a[i][j] = a[n-i+1][j] = 1;}int main(){    int n;    while(~scanf("%d",&n))    {        Init(n);        int tp1 = 1, tp2 = 2;        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                if(a[i][j])                {                    cout<<tp1<<" ";                    tp1 += 2;                }                else                {                    cout<<tp2<<" ";                    tp2 += 2;                }            }            cout<<endl;        }    }    return 0;}
0 0