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 给出 ...
随机推荐
- [原创]java WEB学习笔记67:Struts2 学习之路-- 类型转换概述, 类型转换错误修改,如何自定义类型转换器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- c标签 总结
JSTL标签用法 关键字:JSTL标签.<c:choose>.<c:forEach>.<c:forTokens>.<c:if>.<c:import ...
- 当执行php脚本时用户关闭浏览器会发生什么?
2008年8月16日 1,152 views 发表评论 阅读评论 如果一段php脚本执行插入数据到mysql的操作. 一般情况下,由于php脚本在服务器上执行,此时用户虽然关闭了浏览器,但是服务器端的 ...
- java文件下载 rest
/** * 返回文件二进制 * */ @GET @Path("/excel") @Produces("application/vnd.ms-excel; charset= ...
- PAT乙级 1020. 月饼 (25)(只得到23分)
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- 为centos添加额外的源
使用这个命令: yum install epel-release
- Java中的get()和set()方法
对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 如果你对于这两个方法还有困惑甚至完全不知道这两个方法是做什么的, ...
- 使用node-webkit开发exe窗口程序
首发:个人博客,更新&纠错&回复 ====关于原生程序与壳中程序的议论begin==== 在所有用户windows机器上都能直接跑的程序,如果不采用微软系的语言,如VB,C++,C#等 ...
- :not(selector)
描述: 用于筛选的选择器 查找所有未选中的 input 元素 HTML 代码: <input name="apple" /> <input name=" ...
- mysql单表查询&&多表查询(职员表14+9)
dept(deptno,dname,loc) emp(empno,ename,job,mgr,hiredate,sal,COMM,deptno) salgrade(grade,losal,hisal) ...