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 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.
1
1
3
2 1 4
3 5 7
6 9 8
题解:构造一个魔方(对角,列,行相等)
解法:
如3×3的魔方阵:
8 1 6
3 5 7
4 9 2
魔方阵的排列规律如下:
(1)将1放在第一行中间一列;
(2)从2开始直到n×n止各数依次按下列规则存放;每一个数存放的行比前一个数的行数减1,列数加1(例如上面的三阶魔方阵,5在4的上一行后一列);
(3)如果上一个数的行数为1,则下一个数的行数为n(指最下一行);例如1在第一行,则2应放在最下一行,列数同样加1;
(4)当上一个数的列数为n时,下一个数的列数应为1,行数减去1。例如2在第3行最后一列,则3应放在第二行第一列;
(5)如果按上面规则确定的位置上已有数,或上一个数是第一行第n列时,则把下一个数放在上一个数的下面。例如按上面的规定,4应该放在第1行第2列,但该位置已经被占据,所以4就放在3的下面;
#include<bits/stdc++.h>
using namespace std;
int a[50][50];
int i,j,k,p,n;
int main()
{
cin>>n;
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
a[i][j]=0;
}
}
p=1;
j=n/2+1;
a[1][j]=1;
for (k=2; k<=n*n; k++)
{
i=i-1;
j=j+1;
if ((i<1)&&(j>n))
{
i=i+2;
j=j-1;
}
else
{
if(i<1)
{
i=n;
}
if(j>n)
{
j=1;
}
}
if(a[i][j]==0)
{
a[i][j]=k;
}
else
{
i=i+2;
j=j-1;
a[i][j]=k;
}
}
for(i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
{
if(j!=n)
{
printf("%d ",a[i][j]);
}
else
{
printf("%d",a[i][j]);
}
}
printf("\n");
}
return 0;
}
Educational Codeforces Round 16 C的更多相关文章
- [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]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [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 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Educational Codeforces Round 16
A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x > ...
- Educational Codeforces Round 16 A B C E
做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...
随机推荐
- UML: 部署图
说部署图之前,先看看某24小时便利店管理系统的网络拓扑结构图: 这个图描述了本系统的整体物理结构,从该图我们可以得到以下信息:1.该便利店集团有总部和多个门店,总部管理财务.仓库.采购等事宜.2.二级 ...
- ofbiz进击 个人遇到的奇葩问题汇总。
在本人做退货单生成的时候,因为考虑到要控制通过java类方法去调用 service服务可以方便给出提示消息,所以专门新建了一个java类,然后去重新请求request请求,下面为Java类的代码 pu ...
- android 添加背景音乐
MediaPlayer mediaPlayer=MediaPlayer.create(MainActivity.this,R.raw.qiji); mediaPlayer.start();
- Servlet里写验证码
public class CodeServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * ...
- php操作oracle的方法类集全
在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...
- 夺命雷公狗ThinkPHP项目之----企业网站26之网站前台列表页的显示和完成分页功能
我们用大I接收到我们get过来的栏目页的id然后通过文章的ar_cateid 来判断是不是属于该栏目下的,如果文章表ar_cateid = 栏目表的cate_id 那么就可以选出我们要查找的信息, 然 ...
- 关于teleport_pro使用过程中的一点疑惑
在我新建工程的时候,有两个选项,一个是"new project wizard"另一个是"new project",然后就纠结了,我应该使用那个呢? 使用第一个的 ...
- android 学习随笔十一(网络:HttpClient框架)
1.使用HttpClient框架发送get.post请求 google收集apache提供的一个发送Http请求框架 public class Tools { public static String ...
- android 学习随笔七(网络:图片及文本传输及线程关系 )
主线程.子线程.UI的关系 简单的HTTP请求 -------------------------------------------------------- public class MainAc ...
- 【python cookbook】【数据结构与算法】18.将名称映射到序列的元素中
问题:希望通过名称来访问元素,减少结构中对位置的依赖性 解决方案:使用命名元组collections.namedtuple().它是一个工厂方法,返回的是python中标准元组类型的子类,提供给它一个 ...