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

大体题意:

给你一个奇数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 4
3 5 7
6 9 8


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值