题目链接:

C. Magic Odd Square

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]的全排列,要求所有行所有列的和为奇数;n为奇数; 思路: 可以发现n为奇数的时候就是每行每列的个数都是奇数,所有每行每列里面的奇数的个数都是奇数,所有就可以想到中间是一个奇数组成的45度的正方形,其他是偶数;正好用完了所有的偶数和奇数; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=3e5+10;
const int maxn=1e3+20;
const double eps=1e-12; int ans[50][50]; int main()
{ int n;
read(n); int cnt1=1,cnt2=2,cx=n/2+1,cy=n/2+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(abs(cx-i)+abs(cy-j)<=n/2)ans[i][j]=cnt1,cnt1+=2;
else ans[i][j]=cnt2,cnt2+=2;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<n;j++)printf("%d ",ans[i][j]);
printf("%d\n",ans[i][n]);
} return 0;
}

  

codeforces 710C C. Magic Odd Square(构造)的更多相关文章

  1. codeforces 710C Magic Odd Square(构造或者n阶幻方)

    Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both ma ...

  2. CodeForces - 710C Magic Odd Square(奇数和幻方构造)

    Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, c ...

  3. Codeforces 710C. Magic Odd Square n阶幻方

    C. Magic Odd Square time limit per test:1 second memory limit per test:256 megabytes input:standard ...

  4. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. 【模拟】Codeforces 710C Magic Odd Square

    题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. ...

  6. CodeForces 710C Magic Odd Square

    构造. 先只考虑用$0$和$1$构造矩阵. $n=1$,$\left[ 1 \right]$. $n=3$,(在$n=1$的基础上,最外一圈依次标上$0$,$1$,$0$,$1$......) $\l ...

  7. CodeForces 710C Magic Odd Square (n阶奇幻方)

    题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.co ...

  8. Magic Odd Square (思维+构造)

    Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both ma ...

  9. 689D Magic Odd Square 奇数幻方

    1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行 ...

随机推荐

  1. 2016 acm香港网络赛 C题. Classrooms(贪心)

    原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and ...

  2. Java是否存在内存泄露

    会的. 原因:长生命周期的对象持有短生命周期对象的引用,导致短生命周期对象不能被回收,由此可能发生内存泄露. 举例参考:http://blog.csdn.net/yakihappy/article/d ...

  3. 批量删除redis某个键值

    /usr/local/redis/src/redis-cli -h 172.28.6.23  -p 6383 keys "virtual*" |xargs /usr/local/r ...

  4. javascript修改图片链接地址

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  5. 阿里巴巴fastjson 包的使用解析json数据

    Fastjson是一个Java语言编写的高性能功能完善的JSON库.由阿里巴巴公司团队开发的. 主要特性主要体现在以下几个方面: 1.高性能 fastjson采用独创的算法,将parse的速度提升到极 ...

  6. Linux c编程:同步属性

    就像线程具有属性一样,线程的同步对象(如互斥量.读写锁.条件变量.自旋锁和屏障)也有属性 1.互斥量属性 用pthread_mutexattr_init初始化pthread_mutexattr_t结构 ...

  7. IntelliJ IDEA 添加类注释模板

    效果展示 /** * Created with IntelliJ IDEA * USER:jacun * CLASSNAME: HalloWorldController * DATE: 2019/1/ ...

  8. mongodb 的注意点

    昨天同事安装mongodb遇到了些问题,问了下我,后拉发现都是些细节没注意(讲道理这应该是很简单,一顿操作就ok的事情) 首先,下载 mongo包, 然后 ,解压安装, 启动之. 问题就出现在他后台启 ...

  9. 了解CentOS服务器的基本信息

    简单描述了如何从CPU.内存.硬盘性能.负载方面去了解自己工作的服务器性能.这个很重要,必须了解机器的方方面面才能提高在自己运维工作效率. 一.查看linux服务器cpu详情 查看物理cpu个数: [ ...

  10. Python日期和时间

    日期和时间主要有两个库,datetime和time. datetime: 日期:datetime.date.today() 日期和时间:datetime.datetime.now() 1000天之后: ...