codeforces 710C Magic Odd Square(构造或者n阶幻方)

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 43 5 76 9 8分析:给你与1个奇数n, 让你构造一个n * n 的矩阵,要求保证这个矩阵的每行每列和主对角线上数字的和为奇数。构造:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int ans[50][50];
 4
 5 int main()
 6 {
 7     int n;
 8     ios::sync_with_stdio(false);
 9     cin.tie(0);
10     cin >> n;
11     memset(ans,0,sizeof(ans));
12     int num1 = 1,num2 =2,cnt1 = (n+1)/2,cnt2 = (n+1)/2;
13     for(int i = 1; i <= n; i++)
14     {
15         for(int j = 1; j <= n; j++)
16         {
17             if(abs(cnt1 - i) + abs(cnt2 - j) <= n/2) // 为什么这么写,有点不清楚
18                 ans[i][j] = num1,num1 += 2;
19             else
20                 ans[i][j] = num2,num2 += 2;
21         }
22     }
23     for(int i = 1; i <= n; i++)
24     {
25         for(int j = 1; j< n; j++)
26         {
27             printf("%d ",ans[i][j]);
28         }
29         printf("%d\n",ans[i][n]);
30     }
31     return 0;
32 }

n阶幻方:

 1 # include <stdio.h>
 2 # include <string.h>
 3 int g[50][50];
 4 int main(){
 5     int i, j, k, n, x, y, r, c;
 6     memset(g, 0, sizeof(g));
 7     scanf("%d", &n);
 8     r=1; c=(1+n)/2;
 9     g[1][c]=1;
10
11     for(i=2; i<=n*n; i++){
12         x=r;y=c;
13         x=x-1;
14         if(x<1){
15             x=n;
16         }
17         y=y+1;
18         if(y>n){
19             y=1;
20         }
21         if(g[x][y]){
22             g[r+1][c]=i;
23             r=r+1;
24         }
25         else{
26             g[x][y]=i;
27             r=x;c=y;
28         }
29     }
30     for(i=1; i<=n; i++){
31         for(j=1; j<=n; j++){
32             if(j!=n)
33             printf("%d ", g[i][j]);
34             else{
35                 printf("%d\n", g[i][j]);
36             }
37         }
38
39     }
40     return 0;
41 }  
时间: 2024-11-06 18:56:15

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

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, 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

【模拟】Codeforces 710C Magic Odd Square

题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. 题目思路: [模拟] 分为奇幻方.单偶幻方和双偶幻方三种构造. 具体分类可以查看百度.幻方的N种构造方法 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algori

CodeForces 710C Magic Odd Square

构造. 先只考虑用$0$和$1$构造矩阵. $n=1$,$\left[ 1 \right]$. $n=3$,(在$n=1$的基础上,最外一圈依次标上$0$,$1$,$0$,$1$......) $\left[ {\begin{array}{*{20}{c}}0&1&0\\1&1&1\\0&1&0\end{array}} \right]$. $n=5$,(在$n=3$的基础上,最外一圈依次标上$1$,$0$,$1$,$0$......) $\left[ {\b

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

题意:给它定一个n,让你输出一个n*n的矩阵,使得整个矩阵,每行,每列,对角线和都是奇数. 析:这个题可以用n阶奇幻方来解决,当然也可以不用,如果不懂,请看:http://www.cnblogs.com/dwtfukgv/articles/5797527.html 剩下的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <str

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

Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16

传送门 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 dif

Java 实现奇数阶幻方的构造

一.设计的流程图如下所示 二.Java 语言的代码实现 package MagicSquare; //奇数幻方的实现 public class Magic_Odd { //n 为幻方的阶数 public static int[][] magicOdd(int n) { //构造一个(n+2)*(n+2)阶的方阵 int[][] square = new int[n + 1][n + 1]; int i = 0; int j = (n + 1) / 2; //从第一行的中间那个数字(是1)开始填幻

Codeforces 429B Working out bfs构造

题目链接:点击打开链接 题意:给定n*m的矩阵 有一个人a从左上角走到右下角,只能↓或→走 另一个人b从左下角走到右上角,只能↑或→走 使得2个人的路径有且仅有一个格子是相交的. 统计2个人的权值和(相交格子的权值和不计) 问最大的权值和是多少. 思路: 首先转换一下题意,也就是找一个格子与4个角落连不相交的线. 我们观察相交的那个格子,那个格子的上下左右必然对应着一个角落. (i,j)点,那么(i-1,j)必然对应左上角或右上角的其中一个角落. 这样(i,j)点的4个相邻格子各自对应一个角落(

Codeforces 346C Number Transformation II 构造

题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #define R(x) (x<<