[Educational Codeforces Round 16]C. Magic Odd Square
[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).
输出
输入示例
输出示例
数据规模及约定
见“输入”
题解
构造幻方即可。上网搜一下 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的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [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 ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 16
A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x > ...
- Educational Codeforces Round 9 F. Magic Matrix 最小生成树
F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- 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 ...
- Educational Codeforces Round 8 D. Magic Numbers
Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...
随机推荐
- Android--按钮点击事件
Android中Button的点击事件非常简单,主要是一个内部类的问题 在界面上存在两个按钮和一个文本框,点击不同按钮的时候文本框中显示不同按钮的文字信息 <?xml version=" ...
- 【BZOJ 1019】【SHOI2008】汉诺塔(待定系数法递推)
1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 559 Solved: 341[Submit][Status] ...
- 编写高质量代码改善C#程序的157个建议[匿名类型、Lambda、延迟求值和主动求值]
前言 从.NET3.0开始,C#开始一直支持一个新特性:匿名类型.匿名类型由var.赋值运算符和一个非空初始值(或以new开头的初始化项)组成.匿名类型有如下基本特性: 1.既支持简单类型也支持复杂类 ...
- xml基本操作
在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...
- iOS不得姐项目--登录模块的布局,设置文本框占位文字颜色,自定义内部控件竖直排列的按钮
一.登录模块的布局 将一整部分切割成若干部分来完成,如图分成了三部分来完成 设置顶部状态栏为白色的方法 二.设置文本框占位文字颜色 <1>方法一与方法二实现原理是同一种,都是通过设置pla ...
- iOS开发小技巧--边接受数据边写入文件的两种方法
一.NSFileHanle 使用注意点:在往文件写入数据时,必须创建一个空的文件 指定文件写入的方式 -- 覆盖还是追加 最后记得关闭 <1>代码是在大文件传输的练习中截取的.写入数据之前 ...
- 【HTTP劫持和DNS劫持】腾讯的实际业务分析
简单介绍一下HTTP劫持和DNS劫持的概念,也就是运营商通过某些方式篡改了用户正常访问的网页,插入广告或者其他一些杂七杂八的东西. 首先对运营商的劫持行为做一些分析,他们的目的无非 ...
- C# 中的多线程
参考网站http://blog.gkarch.com/topic/threading.html
- Linux登录验证机制、SSH Bruteforce Login学习
相关学习资料 http://files.cnblogs.com/LittleHann/linux%E4%B8%AD%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E8%AE% ...
- Eclipse启动Tomcat错误:Several ports (8005,8009) required by Tomcat v6.0 Server at localhost are already
解决办法: 1.netstat -aon|findstr 8005 可查看指定端口号使用情况 2.tasklist |findstr 10452 找出占用指定进程Id的程序 3.taskkill /p ...