[Educational Codeforces Round 16]C. Magic Odd Square

时间:2023-01-30 15:18:09

[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. &lbrack;Educational Codeforces Round 16&rsqb;E&period; Generate a String

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

  2. &lbrack;Educational Codeforces Round 16&rsqb;D&period; Two Arithmetic Progressions

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

  3. &lbrack;Educational Codeforces Round 16&rsqb;B&period; 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. &lbrack;Educational Codeforces Round 16&rsqb;A&period; 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 &gt ...

  6. Educational Codeforces Round 9 F&period; 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&period; 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&period; Magic Numbers

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

随机推荐

  1. 给 DevOps 初学者的入门指南

    当我们谈到 DevOps 时,可能讨论的是:流程和管理,运维和自动化,架构和服务,以及文化和组织等等概念.那么,到底什么是"DevOps"呢? 什么是DevOps 随着软件发布迭代 ...

  2. ECharts 初体验

    缘起 ECharts,缩写来自Enterprise Charts,商业级数据图表,它最初是为了满足公司商业体系里各种业务系统(如凤巢.广告管家等等)的报表需求.以前这些系统的图表需求我们都是使用fla ...

  3. 转:使用Nlog记录日志到数据库

    原文:http://www.cnblogs.com/Gyoung/archive/2012/10/18/2729613.html Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台, ...

  4. zabbix windows angent安装:

    zabbix windows angent安装:1.下载zabbix agent for windows客户端,直接解压到C盘下.C:\zabbix 的目录015/04/21 11:16 <DI ...

  5. JavaScript 轮播图实例

    HTML代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  6. User-Agent-Switcher和fiddler

    浏览器模拟器(可以模拟各种浏览器效果,浏览器中看手机显示的效果) http://chromecj.com/web-development/2014-09/70.html User-Agent-Swit ...

  7. 【转】如何将MySQL数据目录更改为CentOS 7上的新位置

    本文转载自:http://www.leftso.com/blog/362.html 介绍 数据库随着时间的推移而增长,有时超过了文件系统的空间.当它们与操作系统的其他部分位于同一分区上时,也可能遇到I ...

  8. ubuntu18&period;04 LTS解决&sol;boot空间不足

    1. 查看磁盘占用情况 df -h Filesystem      Size  Used Avail Use% Mounted on/dev/nvme0n1p5  181M  141M   27M  ...

  9. Python类的进阶&period;md

    属性绑定 在python中可以给类对象动态的绑定属性 但是由于这种特性,随意动态绑定也会带来麻烦,因此可用__slots__来限制可绑定的属性名称 __slots__的绑定对于子类是不生效的,只对当前 ...

  10. mysql中InnoDB表为什么要建议用自增列做主键

    InnoDB引擎表的特点 1.InnoDB引擎表是基于B+树的索引组织表(IOT) 关于B+树 (图片来源于网上) B+ 树的特点: (1)所有关键字都出现在叶子结点的链表中(稠密索引),且链表中的关 ...