codeforces 710C 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.
The only line contains odd integer n (1 ≤ n ≤ 49).
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.
1
1
3
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(构造)的更多相关文章
- 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 ...
- 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 ...
- Codeforces 710C. Magic Odd Square n阶幻方
C. Magic Odd Square time limit per test:1 second memory limit per test:256 megabytes input:standard ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- 【模拟】Codeforces 710C Magic Odd Square
题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. ...
- CodeForces 710C Magic Odd Square
构造. 先只考虑用$0$和$1$构造矩阵. $n=1$,$\left[ 1 \right]$. $n=3$,(在$n=1$的基础上,最外一圈依次标上$0$,$1$,$0$,$1$......) $\l ...
- CodeForces 710C Magic Odd Square (n阶奇幻方)
题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.co ...
- 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 ...
- 689D Magic Odd Square 奇数幻方
1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行 ...
随机推荐
- output的使用
如果现在让你做一个滑动的效果 然后在右侧显示滑动停止以后的数值 那么很多人会选择input 和output 来使用 <!DOCTYPE html> <html lang=" ...
- 九度OJ 1325:Battle Over Cities(城市间的战争) (并查集)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:376 解决:132 题目描述: It is vitally important to have all the cities connect ...
- iOS框架你了解多少?
1.iOS 系统可以分为以下四层,每个框架对应IOS系统里的一层,每层建立在它下面层的上面.应该尽量使用上层的框架来代替下面的框架.更高层次的框架是对底层框架基于对象的抽象.以下列出几个iOS开发的常 ...
- 【python】-- 字符串、字符编码与转码
字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可:访问子字符串,可以使用方括号来截取字符串: var1 ...
- Maven下载、安装和配置(转发:http://blog.csdn.net/jiuqiyuliang/article/details/45390313)
准备工作 java开发环境(JDK) maven下载地址:http://maven.apache.org/release-notes-all.html 安装 安装maven超级简单,总共分四步: 下载 ...
- linux 5-sort,uniq,tar,split
十二. 行的排序命令sort: 1. sort命令行选项: 选项 描述 -t 字段之间的分隔符 -f 基于字符排序时忽略大小写 -k 定义排序的域字段,或者是基于域字段的部分数据进行排序 - ...
- MainWindows
开发带有菜单栏状态栏等常用windows应用时候使用
- LyX中文配置
环境:OS X 10.9; MacTeX-2014; LyX Version 2.1.0 LyX是一个“WYSIWYM”(What You See Is What You Mean)的文字排版系统.其 ...
- oc中的blocks的功能,一种比代理简洁的方式
blocks方式: 谁要东西谁就要写blocks,通过blocks(返回值)获取想要的,提供东西的人要通过实现函数指针把东西给想要的人.当然也可以传值,传值需要通过形参. block的功能: bloc ...
- OSI模型网络分层
OSI TCP/IP --- ------- 应用层 表示层 应用层 会话层 ----- ------- 传输层 TCP UDP ----- ------- 网络层 IPv4/IPv6 ----- - ...