题目链接:

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. python 学习2 测试报告

    1. py.test test_class.py  --resultlog=./log.txt 2.生成html格式 py.test test_class.py --html=./report.htm ...

  2. Linux tar包安装Nginx

    1.首先安装依赖包(依赖包有点多.我们採用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...

  3. GT背靠背onsite

    Google: 因为暑假在G家实习过,所以仅仅是简单面了2轮. 后来跟曾经的intern host吃饭得知,他和还有一个reviewer对我的实习工作都给了不错的评价,所以面试的时候面试官都放水了.题 ...

  4. 2016 acm香港网络赛 B题. Boxes

    原题网址:https://open.kattis.com/problems/boxes Boxes There are N boxes, indexed by a number from 1 to N ...

  5. 第4章 URL管理器和实现方法

    URL管理器:管理待抓取URL集合和已抓取URL集合 -- 防止重复抓取.防止循环抓取 URL需要支持哪些功能: 添加新URL到待爬取集合中.判断待添加URL是否在容器中,判断是否还有待爬取URL,获 ...

  6. WebApi 中使用 Session

    1. 在 Global.asax.cs 文件中加入session支持 protected void Application_Start() { AreaRegistration.RegisterAll ...

  7. 记录-MySQL中的事件调度Event Scheduler

    下面是自己的实例 /*查询event是否开启(查询结果Off为关闭 On为开启)*/show variables like '%sche%'; /*开启/关闭命令(1开启--0关闭)*/set glo ...

  8. python的进程和线程

    关于进程: An executing instance of a program is called a process.程序的执行实例称为进程. Each process provides the ...

  9. Java 重写 equals 与 hashCode 的注意事项

    为什么重写 equals 的时候必须重写 hashCode 大家可能从很多教程中了解到: SUN官方的文档中规定"如果重定义equals方法,就必须重定义hashCode方法,以便用户可以将 ...

  10. Linux里的发消息

    1.给指定用户发送消息 语法: write 用户名 说明: 1.用户是在线的 2.执行过程 a.敲完命令按回车,进入写信模式 b.写信的时候如果写错了Ctrl+退格 删除字符 c.写完以后Ctrl+D ...