CodeForces 710C Magic Odd Square (n阶奇幻方)
题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数。
析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.com/dwtfukgv/articles/5797527.html
剩下的就很简单了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline LL Max(LL a, LL b){ return a < b ? b : a; }
inline LL Min(LL a, LL b){ return a > b ? b : a; }
inline int Max(int a, int b){ return a < b ? b : a; }
inline int Min(int a, int b){ return a > b ? b : a; } int a[55][55];
int main(){
while(scanf("%d", &n) == 1){
int j = n/2, num = 1, i = 0;
while(num != n*n + 1){
int ii = (i % n + n) % n;
int jj = (j % n + n) % n;
a[ii][jj] = num;
if(num % n == 0) ++i;
else --i, ++j;
++num;
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(j == n-1) printf("%d\n", a[i][j]);
else printf("%d ", a[i][j]);
}
return 0;
}
CodeForces 710C Magic Odd Square (n阶奇幻方)的更多相关文章
- Codeforces 710C. Magic Odd Square n阶幻方
C. Magic Odd Square time limit per test:1 second memory limit per test:256 megabytes input:standard ...
- 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阶幻方)
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
题目链接: 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 C. Magic Odd Square(构造)
题目链接: C. Magic Odd Square Find an n × n matrix with different numbers from 1 to n2, so the sum in ea ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- 689D Magic Odd Square 奇数幻方
1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行 ...
- 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 ...
随机推荐
- 用sql获取某字符串中的数字部分的语句
create function dbo.F_Get_No ( @No varchar(100) ) RETURNS bigint AS BEGIN WHILE PATINDEX('%[^0-9]%', ...
- NBUT 1121 Sakuya's Fly Knife 飞刀(暴力)
题意:给出一个带有n*m个格子的矩阵,部分格子中有靶子target,现在要从一个没有靶子的格子中射出飞刀数把,飞刀是可穿透靶子的,同一直线上都可以一刀全射掉.现在问在哪个格子射出飞刀,可以在全部射中的 ...
- mysql装载本地文件及模式匹配
使用load data装载本地文件到表中,文件每行一条记录,列值之间用tab分隔,并按照次序一一列出,对于无值或丢失的情况可以使用\N.但是在使用insert into插入的时候不能使用\N,而应该是 ...
- [asp.net] 通过JS实现对treeview控件的复选框单选控制。
前端JS代码: //识别不同的浏览器 function getTargetElement(evt) { var elem if (evt.target) { elem = (evt.target.no ...
- Excel 之查找与替换
Excel查找与替换 1,一旦学会查找,替换就简单了. 2,查找下面有一个选项,里面有对查找的范围进行限制 3,你可以选中一个区域,然后再查找,查找只会在你选择的区域里面进行 4,查找只能找特定值,而 ...
- VPS技术介绍以及分析
VPS的全称为Virtual Private Server,叫做虚拟专用服务器(Godaddy称之为Virtual Dedicated Server,VDS).就是利用各种虚拟化手段把单台物理服务器虚 ...
- yii2.0 输出url 注册js css文件
//输出url <a href="<?= Url::to(['/users/login/login','id'=>5,'mark'=>true]) ?>&qu ...
- JVM——类加载机制
虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 在Java语言中,类型的加载.连接和初始化过 ...
- 【转】Git连接oschina管理代码版本
原文网址:http://blog.csdn.net/liukang325/article/details/24051467 工作中一般都是用的SVN,最近好像GitHub有些火,看到开源中国上也有Gi ...
- Android内存控制小技巧-使用矢量图来节省你的内存并简化你的开发。
先上一个 位图和矢量图的 说明.http://zhidao.baidu.com/link?url=xwvs5CBzWeh15O3Ee4bICwCqg4PCQWwg5oZ0a6CVydbVZzufqrI ...