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
分析:给你与1个奇数n, 让你构造一个n * n 的矩阵,要求保证这个矩阵的每行每列和主对角线上数字的和为奇数。
构造:
 #include<bits/stdc++.h>
using namespace std;
int ans[][]; int main()
{
int n;
ios::sync_with_stdio(false);
cin.tie();
cin >> n;
memset(ans,,sizeof(ans));
int num1 = ,num2 =,cnt1 = (n+)/,cnt2 = (n+)/;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(abs(cnt1 - i) + abs(cnt2 - j) <= n/) // 为什么这么写,有点不清楚
ans[i][j] = num1,num1 += ;
else
ans[i][j] = num2,num2 += ;
}
}
for(int i = ; i <= n; i++)
{
for(int j = ; j< n; j++)
{
printf("%d ",ans[i][j]);
}
printf("%d\n",ans[i][n]);
}
return ;
}

n阶幻方:

 # include <stdio.h>
# include <string.h>
int g[][];
int main(){
int i, j, k, n, x, y, r, c;
memset(g, , sizeof(g));
scanf("%d", &n);
r=; c=(+n)/;
g[][c]=; for(i=; i<=n*n; i++){
x=r;y=c;
x=x-;
if(x<){
x=n;
}
y=y+;
if(y>n){
y=;
}
if(g[x][y]){
g[r+][c]=i;
r=r+;
}
else{
g[x][y]=i;
r=x;c=y;
}
}
for(i=; i<=n; i++){
for(j=; j<=n; j++){
if(j!=n)
printf("%d ", g[i][j]);
else{
printf("%d\n", g[i][j]);
}
} }
return ;
}

codeforces 710C Magic Odd Square(构造或者n阶幻方)的更多相关文章

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

  2. Codeforces 710C. Magic Odd Square n阶幻方

    C. Magic Odd Square time limit per test:1 second memory limit per test:256 megabytes input:standard ...

  3. 【模拟】Codeforces 710C Magic Odd Square

    题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. ...

  4. CodeForces 710C Magic Odd Square

    构造. 先只考虑用$0$和$1$构造矩阵. $n=1$,$\left[ 1 \right]$. $n=3$,(在$n=1$的基础上,最外一圈依次标上$0$,$1$,$0$,$1$......) $\l ...

  5. CodeForces 710C Magic Odd Square (n阶奇幻方)

    题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.co ...

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

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

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

  8. 689D Magic Odd Square 奇数幻方

    1 奇数阶幻方构造法 (1) 将1放在第一行中间一列; (2) 从2开始直到n×n止各数依次按下列规则存放:按 45°方向行走,向右上,即每一个数存放的行比前一个数的行数减1,列数加1 (3) 如果行 ...

  9. Magic Odd Square (思维+构造)

    Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both ma ...

随机推荐

  1. Android群英传-拼图游戏puzzle-代码设计和实现

    上个周末,3个小时总体上读完了<Android群英传>,本周主要在研究代码层次的设计和实现.  编译安装在手机上,玩了几把,结合代码,一周时间才掌握了整体的思路.  大部分时间,其实花在了 ...

  2. Linux修改Linux默认打开方式

    从总体上讲 /etc/gnome/defaults.list 保存了全局的打开方式 /.local/share/applications/mimeapps.list 保存了个人的打开方式当两着不一致是 ...

  3. cocos2dx 使用spine制作骨骼动画

    刚刚接触骨骼动画,所以写一篇文章记录. 1.首先先画好人物的每一个部件: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fon ...

  4. hadoop-10-创建yum资源库

    hadoop-10-创建yum资源库 1,在/etc/yum.repos.d/下面创建 ambari.repo  HDP.repo  HDP-UTILS.repo 三个文件: [root@server ...

  5. bzoj1497【NOI2006】最大获利

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 3437  Solved: 1674 [id=1497" ...

  6. Vim 批量替换

    假设在非Win系统下. 想批量替换文本不再是Ctrl+F那么简单了, 一般用Vim来做批量替换, 略微复杂点: 比如将192.168.0.1替换为192.168.0.2 :%s/192.168.0.1 ...

  7. Linux 文件描写叙述符设置为非堵塞的方法

    通过fcntl设置文件描写叙述符属性 fcntl即F_SETFL,F_GETFL的使用,设置文件的flags,堵塞设置成非堵塞,非堵塞设置成堵塞(这连个在server开发中能够封装为基本函数) 1.获 ...

  8. nj03---阻塞和线程

    Node.js最大的特性就是"异步式I/O"与事件紧密结合的编程模式.这种模式与传统的同步式IO线性的编程思路有很大的不同,因为控制流很大程度上要靠"事件"和& ...

  9. 射击的乐趣:WIN32诠释打飞机游戏

    一楼留给链接http://blog.csdn.net/crocodile__/article/details/11860129 楼上神贴,膜拜片刻...... 一.游戏玩法和已经实现的功能 1.打开游 ...

  10. GoldenGate 日常监控

    正确启动数据库 源端启动数据库 SQL>  startup 源端启动goldengate GGSCI >  start mgr GGSCI >  start * 目标端启动数据库 S ...