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.

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

题解:构造一个魔方(对角,列,行相等)

解法:

如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的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [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 ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  6. 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 ...

  7. Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)

    Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...

  8. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

  9. Educational Codeforces Round 16

    A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x > ...

  10. Educational Codeforces Round 16 A B C E

    做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...

随机推荐

  1. android测试(转)

    1.冒烟测试 跟web端的测试流程一样,你拿到一个你们开发做出来的apk首先得去冒烟,也就是保证他的稳定性,指定时间内不会崩溃.这款原生sdk自带的monkey可以当做我们的测试工具.就跟我之前博客所 ...

  2. springday03-go2

    新建springmvc01项目1.创建项目,导入jar包 拷贝jar/spring/first下的五个spring的jar包,以及jar/spring/mvc下的两个mvcjar包放在lib下 2.创 ...

  3. ajax“显示弹窗详情”和“删除”功能练习

    1.查看详细信息,以弹窗的形式显示,使用ajax 2.批量删除 “查询”功能可以参考前面的文章,这里只讲解ajax“显示弹窗详情”和“删除”功能 第一:在body中的代码 <title>a ...

  4. C#学习总结~~~

    0.和java很相似, struct(结构) 实例化出来的对象,是在内存栈中分配: class(类)实例化出来的对象,指向了内存堆中分配的空间:  string.object.class,这3个引用类 ...

  5. HTML5,添加图片

    <img src="0.jpg" width="100"  height="150" alt="11">

  6. Yii2下拉框实现

    详细介绍yii2下拉框的实现方法,以商品分类的下拉框为例: 第一种方法:使用Html的activeDropDownList(),该方法的优点是:可以自定义下拉框的样式.具体实现如下: 1.控制器中,获 ...

  7. PHP 文件上传的综合实例

    1.upload.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <htm ...

  8. COM编程之一 组件

    [1]组件产生的背景 一个应用程序通常是由单个二进制文件组成的. 当应用程序版本发布后一般不会发生任何变化,对于操作系统.硬件以及客户需求的改变都必须要等到修复源代码后且整个应用程序被重新编译才可处理 ...

  9. android 学习随笔十二(网络:使用异步HttpClient框架)

    使用异步HttpClient框架发送get.post请求 在https://github.com/ 搜索 asyn-http https://github.com/search?utf8=✓& ...

  10. 鸟哥的linux私房菜之vim

    vim是vi的进阶版本