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. 面试题——ArrayList和LinkedList的区别

    List概括 先回顾一下List在Collection的框架图: 从图中可以看出: List是一个接口,他继承Collection接口,代表有序的队列. AbstractList是一个抽象类, ,它继 ...

  2. Precision and recall From Wiki

    Precision.全部推断为正样本的数量里面,有多少是真正的正样本.就是精确率 Recall.所有的正样本里面,检測到了多少真正的正样本,又称查全率.即所有正样本查找到了多少的比率.

  3. MYSQL学习笔记三:日期和时间函数

    MYSQL学习笔记三:日期和时间函数 1. 获取当前日期的函数和获取当前时间的函数 /*获取当前日期的函数和获取当前时间的函数.将日期以'YYYY-MM-DD'或者'YYYYMMDD'格式返回 */ ...

  4. lua实现大数运算

    lua实现的大数运算,代码超短,眼下仅仅实现的加减乘运算 ------------------------------------------------ --name: bigInt --creat ...

  5. 高速掌握Lua 5.3 —— I/O库 (1)

    Q:什么是"Simple Model"? A:全部的文件操作都基于一个默认的输入文件和一个默认的输出文件.这就意味着同一时间对于输入和输出来说,仅仅可操作一个文件(默认的文件). ...

  6. hdu-2871

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  7. win7笔记本设置wifi热点

    1.打开cmd 输入netsh wlan set hostednetwork mode=allow ssid=ACE-PC key=12345678 2.等待1-2分钟后,网络连接里会出现一个&quo ...

  8. 人生之清单(list of life)

    人生要走走停停,想想记记,修修改改,再向前走.国家工程,公平正义,一腔热血,都很容易燃起来,热血起来. 1. 没得选,有得选,怎么办 没得选 原生家庭,自己怎么生,怎样的父母: 遇见谁,会有怎样的对白 ...

  9. 图论之堆优化的Prim

    本题模板,最小生成树,洛谷P3366 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边 ...

  10. 【转】android新组件RecyclerView使用介绍和进阶使用,替用Gallery

    简介: RecyclerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,但是直接把viewholder的实现封装起来,用 ...