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.
Input
The only line contains odd integer n (1 ≤ n ≤ 49).
Output
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.
Examples
Input
1
Output
1
Input
3
Output
2 1 4
3 5 7
6 9 8
这个题在中间的菱形区域是奇数,其余为偶数
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const int maxn=3e5+5;
typedef long long ll;
using namespace std;
int a[maxn];
int b[maxn];
int Map[200][200];
int main()
{
int n;
cin>>n;
memset(Map,0,sizeof(Map));
int cnt1=0;
int cnt2=0;
for(int t=1;t<=n*n;t++)
{
if(t%2==0)
{
a[cnt1++]=t;
}
else
{
b[cnt2++]=t;
}
}
int cnt3=0;
for(int t=0;t<n;t++)
{
for(int j=0;j<n;j++)
{
if(j>=n/2-t&&j<=t+n/2&&j<=n-1+n/2-t&&j>=t-n/2)
{
Map[t][j]=b[cnt3];
cnt3++;
}
}
}
int cnt4=0;
for(int t=0;t<n;t++)
{
for(int j=0;j<n;j++)
{
if(Map[t][j]==0)
{
Map[t][j]=a[cnt4];
cnt4++;
}
}
}
for(int t=0;t<n;t++)
{
for(int j=0;j<n;j++)
{
cout<<Map[t][j]<<" ";
}
cout<<endl;
}
}
Magic Odd Square (思维+构造)的更多相关文章
- 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 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 ...
- 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(构造或者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 ...
- 689D Magic Odd Square 奇数幻方
1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行 ...
- CF710C Magic Odd Square 题解
Content 构造出一个 \(n\times n\) 的矩阵,使得这个矩阵由 \(1\sim n^2\) 这些数字组成,并且这个矩阵的每行,每列,以及对角线的和都为奇数. 数据范围:\(1\leqs ...
随机推荐
- c++ list 容器
List vector在STL中是一个双向链表,它的内部结构和vector 或 deque截然不同.主要有以下几点: 1.List不支持随机存取.list没有提供下标操作符和at()的访问. 2.任何 ...
- Redis02 Redis客户端之Java、连接远程Redis服务器失败
1 查看支持Java的redis客户端 本博文采用 Jedis 作为redis客户端,采用 commons-pool2 作为连接redis服务器的连接池 2 下载相关依赖与实战 2.1 到 Repos ...
- 为Vmware里安装的CentOS7.5设置静态IP
[引言]为测试搭建大数据集群环境,采用在Vmware里安装了几台CentOS7.5的虚拟机,在测试过程中,出现启动虚拟机后,虚拟机的IP地址会变,不方便测试集群,所以需要设置静态IP,在此,记录我的设 ...
- 在Win7 64位电脑上安装Sql Server 2008 R2 Express
安装环境说明: 操作系统:Win7 64位 英文版 安装步骤: 1.准备安装文件 下载网址:https://www.microsoft.com/zh-CN/download/details.aspx? ...
- c语言打印空白星号矩形
用户输入一个数字N,输出一个N*N的空心矩形,N最小为3 效果如下: 思路是这样的,首先拿到这道题是没有思路的,但我们可以举几个例子,当N等于3的情况,当N=5的情况,发现第一行和最后一行是相同的,而 ...
- [GO]冒泡排序的原理和代码实现
冒泡排序的原理:对于一个数组里所有的元素进行两两比较,发生大于则变换数组下标则为升序排序,发生小于则变换数据下标的则为降序排序 比如给定的数组为[1, -2, 3, -4],对于我们的需求,两两比较后 ...
- 二度Xml<2>
一下介绍xml的基本操作,添加xml新节点: 其他方法在前一篇日记中有详细讲解,请详见:http://www.cnblogs.com/fjsnail/archive/2012/10/20/273212 ...
- (转)自制AutoMapper实现DTO到持久层Entity的转换
原文地址:http://www.cnblogs.com/qidian10/p/3173907.html 项目中经常涉及到页面DTO更新,保存到数据库的操作,这就必然牵扯到DTO和持久层对象的转换,常见 ...
- 编写高质量代码改善C#程序的157个建议——建议49:在Dispose模式中应提取一个受保护的虚方法
建议49:在Dispose模式中应提取一个受保护的虚方法 在标准的Dispose模式中,真正的IDisposable接口的Dispose方法并没有做实际的清理工作,它其实是调用了下面的这个带bool参 ...
- .net Stream篇(四)
FileStream 目录: 如何去理解FileStream? FileStream的重要性 FileStream常用构造函数(重要) 非托管参数SafeFileHandle简单介绍 FileStre ...