[Educational Codeforces Round 16]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.

输入示例


输出示例


数据规模及约定

见“输入

题解

构造幻方即可。上网搜一下 NOIP2015 Day T1.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 55
int n, A[maxn][maxn]; int main() {
n = read(); int x = 1, y = (n >> 1) + 1, tot = 0;
A[x][y] = ++tot;
while(tot < n * n) {
if(x == 1 && y != n){ x = n; y++; A[x][y] = ++tot; continue; }
if(x != 1 && y == n){ x--; y = 1; A[x][y] = ++tot; continue; }
if(x == 1 && y == n){ x++; A[x][y] = ++tot; continue; }
if(x != 1 && y != n) {
if(!A[x-1][y+1]){ x--; y++; A[x][y] = ++tot; continue; }
x++; A[x][y] = ++tot;
}
} for(int i = 1; i <= n; i++) {
for(int j = 1; j < n; j++) printf("%d ", A[i][j]);
printf("%d\n", A[i][n]);
} return 0;
}

[Educational Codeforces Round 16]C. Magic Odd Square的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  4. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  5. Educational Codeforces Round 16

    A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x > ...

  6. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  7. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  8. Educational Codeforces Round 16 C

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

  9. Educational Codeforces Round 8 D. Magic Numbers

    Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...

随机推荐

  1. Webbench网站压力测试

      Webbench是有名的网站压力测试工具,能测试处在相同硬件上,不同服务的性能以及不同硬件上同一个服务的运行状况.webBech的标准测试可以向我们展示服务器的 两项 内容:每秒钟相应请求数和每秒 ...

  2. 每天一个linux命令(25):df 命令

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  3. 由“js跨域”想到"AJAX也不一定要XMLHttpRequest"

    关键字:jsonp jsonp的原理:同源约束限制了js脚本的跨域访问,但是<script>和<iframe>的src标签引用的js文件(只要响应正文是符合js语法的文本即可, ...

  4. java多线程-Condition

    Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对象提供多个等待 set ...

  5. 关于opacity透明度子元素继承现象的若干研究以及hack方法

    [感想]信息时代的信息是有时效性的,今天是确确实实感受到了.互联网资料虽然丰富,但是质量不一,还有大量的跟风雷同,很多人都是随手拷贝过来,根本没有实践.以前端为例,这两年浏览器的迅猛发展,造成很多原有 ...

  6. Qt *.pro编写一般规则

    qmake 之 CONFIG 与 QT 乱谈 看qtcn论坛中经常有人忘记 QT+=network 等语句.随便写写吧,或许对他人有帮助. 写来写去,发现越写越乱,就这样吧,反正主要的内容很简单. d ...

  7. Java设计模式-原型模式(Prototype)

    原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象.这就是选型模式的用意. 原型模式的结构 原型模式要求对象实现一个可以“克 ...

  8. Hibernate-二级缓存 sessionFactory

    Hibernate 二级缓存 二级缓存需要sessionFactory来管理,它是进初级的缓存,所有人都可以使用,它是共享的. 当Hibernate根据ID访问数据对象的时候,首先从Session一级 ...

  9. 【教程】如何正确的写一个Lemon/Cena的SPJ(special judge)

    转自:http://www.cnblogs.com/chouti/p/5752819.html Special Judge:当正确的输出结果不唯一的时候需要的自定义校验器 首先有个框架 #includ ...

  10. Azure怎么使用ftp登录

    1.下载配置文件 2.拷贝FTP的地址 3.查看配置文件里面的用户名和密码 4.登录