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 ...
随机推荐
- 下载tomcat安装版
1.我们首先在浏览器的地址栏中输入下载地址,如下: Tomcat下载网站链接:http://tomcat.apache.org/ 大家注意:以上下载链接中以zip或gz结尾的都是免安装版的,我们要下载 ...
- ROS naviagtion analysis: costmap_2d--Layer
博客转载自:https://blog.csdn.net/u013158492/article/details/50493113 这个类中有一个LayeredCostmap* layered_costm ...
- 使对象可以像数组一样foreach循环,要求属性必须是私有的(写个类实现Iterator接口)
<?php class Test implements Iterator { ,,,,); public function __construct() { } // 重置,将数组内部指针指向第一 ...
- float在内存中的存取方法
今天做了一些题目,想到float数据如何在内存中的形式.不知道一个浮点数是如何存成32位01字符串的.下面是查找的一些资料. 我们先通过java获取这些数的二进制表示. public class De ...
- SpringMVC——<mvc:annotation-driven/>
会自动注 册RequestMappingHandlerMapping .RequestMappingHandlerAdapter 与 ExceptionHandlerExceptionResolver ...
- 软件工程:Java实现WC.exe基本功能
项目相关要求 GitHub地址:https://github.com/3216004716/WC 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处 ...
- 【留用】C#的一些好的书籍
浏览博客的时候发现一篇推荐的C#书籍,感觉真的不错,涉略过几本,水平问题,没看的很深入,正在努力,留用了!!! http://www.cnblogs.com/tongming/p/3879752.ht ...
- sql server 简单语句整合
1.去重distinct , group by select distinct userid,username from 表名 select userid,username from 表名 group ...
- C#ADO.NET基础一
简介 使用SQLite进行讲解 1.基础类: SQLiteConnection 连接数据库 SQLiteCommand 执行命令(增,删,改,查),或存储过程 SQLiteDataReader 读取查 ...
- CentOS 系统管理与yum软件仓库搭建
重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 ...