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 给出 ...
随机推荐
- TTabControl、TMemo组件(制作一个简单的多文本编辑框)
TTabControl包含一列字符串标签的tabs 每个标签控制一个对象 首先创建一个TForm;接下来添加TTabControl组件和一个文件对话框TOpenDialog(用于添加文件),然后在TT ...
- struts局部、全局类型转换器
第01步:编写bean package com.self.bean; import java.util.Date; public class User { private Date birthday ...
- Android -- 初探MVP模式
1,相信大家对mvp模式都很熟悉了,M-Model-模型.V-View-视图.C-Controller-控制器.MVP作为MVC的版本演化,与MVC的意义类似:M-Model-模型.V-View-视图 ...
- android中在代码中设置margin属性
1,不多说,小知识点,直接上代码 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15);// 创 ...
- paper 22:kl-divergence(KL散度)实现代码
这个函数很重要: function KL = kldiv(varValue,pVect1,pVect2,varargin) %KLDIV Kullback-Leibler or Jensen-Shan ...
- paper 8:支持向量机系列五:Numerical Optimization —— 简要介绍求解求解 SVM 的数值优化算法。
作为支持向量机系列的基本篇的最后一篇文章,我在这里打算简单地介绍一下用于优化 dual 问题的 Sequential Minimal Optimization (SMO) 方法.确确实实只是简单介绍一 ...
- PAT乙级 1027. 打印沙漏(20)
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- Auto push git tag
CALL "C:\Program Files\TortoiseGit\bin\pageant.exe" "d:\CKey.ppk" set GIT_SSH=C: ...
- JVM参数配置的线上教训
原来规则处理业务五十台服务器经常大量fgc,load飙高,我修改了jvm配置后,五十台服务器十多天没有任何异常,双十一中轻闲度过. 可是今天突然又有一台大量fgc,load飙高.分析了半天,回头一看, ...
- 鸟哥的linux私房菜之档案与文件系统的压缩与打包
00000001 节约空间 其实简单的说压缩就是把没有用到的0给去掉,解压的时候在加上 在linux中,压缩文件档案的扩展名大多是.tar,.tar.gz,tgz,gz,.Z,.bz2 compres ...