Educational Codeforces Round 16 C. Magic Odd Square
2016-08-25 19:38
495 查看
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
output
input
output
给你一个奇数,让你构造一个n*n的矩阵,矩阵的每一行,每一列还有对角线上数字的和都是奇数。
因为和是奇数,所以每一行每一列还有对角线上的数字的奇数肯定是奇数个。我们用1表示奇数,2表示偶数画出两个矩阵来看一下
n=3时
2 1 2
1 1 12 1 2
n=5时
2 2 1 2 2
2 1 1 1 2
1 1 1 1 12 1 1 1 2
2 2 1 2 2
可以发现到矩阵中心曼哈顿距离小于等于n/2的都是奇数,其他的都是偶数。(其实我是看别人的)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
int n,i,j,x;
while(scanf("%d",&n)==1)
{
int odd = 1,even = 2;
int m = n/2 + 1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(abs(i - m) + abs(j - m) <= n/2)
{
x = odd;
odd+=2;
}
else
{
x = even;
even+=2;
}
printf("%d ",x);
}
printf("\n");
}
}
}
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
给你一个奇数,让你构造一个n*n的矩阵,矩阵的每一行,每一列还有对角线上数字的和都是奇数。
因为和是奇数,所以每一行每一列还有对角线上的数字的奇数肯定是奇数个。我们用1表示奇数,2表示偶数画出两个矩阵来看一下
n=3时
2 1 2
1 1 12 1 2
n=5时
2 2 1 2 2
2 1 1 1 2
1 1 1 1 12 1 1 1 2
2 2 1 2 2
可以发现到矩阵中心曼哈顿距离小于等于n/2的都是奇数,其他的都是偶数。(其实我是看别人的)
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
int n,i,j,x;
while(scanf("%d",&n)==1)
{
int odd = 1,even = 2;
int m = n/2 + 1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(abs(i - m) + abs(j - m) <= n/2)
{
x = odd;
odd+=2;
}
else
{
x = even;
even+=2;
}
printf("%d ",x);
}
printf("\n");
}
}
}
相关文章推荐
- Educational Codeforces Round 16 C. Magic Odd Square
- Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16
- Educational Codeforces Round 16-C. Magic Odd Square
- Educational Codeforces Round 16 C. Magic Odd Square
- Educational Codeforces Round 16 Magic Odd Square
- Educational Codeforces Round 16 C 题 Magic Odd Square
- Educational Codeforces Round 16 C. Magic Odd Square (构造)
- Educational Codeforces Round 16 -- C - Magic Odd Square (找规律)
- 【Educational Codeforces Round 16】 Codeforces 710E Generate a String
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
- Educational Codeforces Round 16 A. King Moves【模拟】
- codeforces Educational Round 16 E. Generate a String
- Codeforces Educational Codeforces Round 16 部分题解
- Educational Codeforces Round 16 B. Optimal Point on a Line【水】
- DP方程——Generate a String ( Educational Codeforces Round 16 E)
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions ★ ★ ★
- Educational Codeforces Round 16
- Educational Codeforces Round 16 E. Generate a String dp
- Educational Codeforces Round 4 E. Square Root of Permutation(置换、构造)
- Educational Codeforces Round 16 E. Generate a String (DP)