[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;前 ...
随机推荐
-
给 DevOps 初学者的入门指南
当我们谈到 DevOps 时,可能讨论的是:流程和管理,运维和自动化,架构和服务,以及文化和组织等等概念.那么,到底什么是"DevOps"呢? 什么是DevOps 随着软件发布迭代 ...
-
ECharts 初体验
缘起 ECharts,缩写来自Enterprise Charts,商业级数据图表,它最初是为了满足公司商业体系里各种业务系统(如凤巢.广告管家等等)的报表需求.以前这些系统的图表需求我们都是使用fla ...
-
转:使用Nlog记录日志到数据库
原文:http://www.cnblogs.com/Gyoung/archive/2012/10/18/2729613.html Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台, ...
-
zabbix windows angent安装:
zabbix windows angent安装:1.下载zabbix agent for windows客户端,直接解压到C盘下.C:\zabbix 的目录015/04/21 11:16 <DI ...
-
JavaScript 轮播图实例
HTML代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
-
User-Agent-Switcher和fiddler
浏览器模拟器(可以模拟各种浏览器效果,浏览器中看手机显示的效果) http://chromecj.com/web-development/2014-09/70.html User-Agent-Swit ...
-
【转】如何将MySQL数据目录更改为CentOS 7上的新位置
本文转载自:http://www.leftso.com/blog/362.html 介绍 数据库随着时间的推移而增长,有时超过了文件系统的空间.当它们与操作系统的其他部分位于同一分区上时,也可能遇到I ...
-
ubuntu18.04 LTS解决/boot空间不足
1. 查看磁盘占用情况 df -h Filesystem Size Used Avail Use% Mounted on/dev/nvme0n1p5 181M 141M 27M ...
-
Python类的进阶.md
属性绑定 在python中可以给类对象动态的绑定属性 但是由于这种特性,随意动态绑定也会带来麻烦,因此可用__slots__来限制可绑定的属性名称 __slots__的绑定对于子类是不生效的,只对当前 ...
-
mysql中InnoDB表为什么要建议用自增列做主键
InnoDB引擎表的特点 1.InnoDB引擎表是基于B+树的索引组织表(IOT) 关于B+树 (图片来源于网上) B+ 树的特点: (1)所有关键字都出现在叶子结点的链表中(稠密索引),且链表中的关 ...