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 ...
随机推荐
- Codeforces 1142D Foreigner (DP)
题意:首先定义了一种类数(标志数) 1:1到9都是标志数. 2:若x / 10是标志数,假设x /10在标志数中的排名是k, 若x的个位数小于k % 11, 那么x也是标志数. 现在给你一个字符串,问 ...
- css二阴影+第三维度z-index
<span style="text-shadow: 3px 3px 1px #888;">shadowed</span><span style=&qu ...
- 409. Longest Palindrome 最长对称串
[抄题]: Given a string which consists of lowercase or uppercase letters, find the length of the longes ...
- asp.net web 自定义控件
0.调用代码 protected override void Page_Load(object sender, EventArgs e) { //给基类服务接口复制,可不付 if (IsPostBac ...
- Linux下的strerror是否线程安全?
下列是glibc-2.14中的源代码: 点击(此处)折叠或打开 char * strerror (errnum) int errnum; { char *ret = __strerror_r (err ...
- 【转】android中layout_weight的理解
android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...
- 没有为扩展名“.cshtml”注册的生成提供程序
新建的mvc4 项目,然后从其他项目里拷贝shared文件夹和_ViewStart.cshtml文件过去,然后在@符号上出现“没有为扩展名“.cshtml”注册的生成提供程序” 解决方法: 需要在项目 ...
- [转]Programmatically Register Assemblies in C#.
1. Introduction. 1.1 After publishing Programmatically Register COM Dlls in C# back in 2011, I recei ...
- 从头开始学eShopOnContainers——开发环境要求
一.简介 eShopOnContainers是一个简化版的基于.NET Core和Docker等技术开发的面向微服务架构的参考应用,是一个简化版的在线商城/电子商务应用,其包含基于浏览器的Web应用. ...
- Autofac的Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误解决方案。
在使用Autofac的时候,不给力,看着例子来的,人家没问题,我就报了Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误. 百般 ...