codeforces 710C Magic Odd Square(规律)
2016-08-23 21:46
302 查看
C. Magic Odd Square
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
题意:让你写出一个矩阵使得它的行、列、对角线上的和为奇数
#include<cstdio>
#include<cstring>
int main()
{
int n;
int map[50][50];
while(scanf("%d",&n) != EOF)
{
memset(map,0,sizeof(map));
if(n == 1)
{
printf("1\n");
continue;
}
else
{
int k = 1;
int f = (n + 1) / 2;
int x = 0 , y = 0;
for(int i = 1 ; i <= n ; i++)
{
if(i <= f)
{
x = f - i + 1;
y = f + i -1;
}
else
{
x = i - f + 1;
y = n - i + f;
}
for(int j = x ; j <= y ; j++)
{
map[i][j] = k;
k += 2;
}
}
}
int l = 2;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++)
{
if(!map[i][j])
{
map[i][j] = l;
l += 2;
}
printf("%d",map[i][j]);
if(j != n)
printf(" ");
else
printf("\n");
}
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 76 9 8
题意:让你写出一个矩阵使得它的行、列、对角线上的和为奇数
#include<cstdio>
#include<cstring>
int main()
{
int n;
int map[50][50];
while(scanf("%d",&n) != EOF)
{
memset(map,0,sizeof(map));
if(n == 1)
{
printf("1\n");
continue;
}
else
{
int k = 1;
int f = (n + 1) / 2;
int x = 0 , y = 0;
for(int i = 1 ; i <= n ; i++)
{
if(i <= f)
{
x = f - i + 1;
y = f + i -1;
}
else
{
x = i - f + 1;
y = n - i + f;
}
for(int j = x ; j <= y ; j++)
{
map[i][j] = k;
k += 2;
}
}
}
int l = 2;
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++)
{
if(!map[i][j])
{
map[i][j] = l;
l += 2;
}
printf("%d",map[i][j]);
if(j != n)
printf(" ");
else
printf("\n");
}
}
return 0;
}
相关文章推荐
- linux下衡量cpu性能的bogomips
- 数据库范式
- 顺序表应用6:有序顺序表查询
- Codeforces Round #364 (Div. 2)->A. Cards
- 继承
- JSTL遍历数组
- hrbust 1759 Lawnmower【思维题】
- 使用jquery操作session
- 【Light-oj】-1043 - Triangle Partitioning(数学)
- maven
- 【LightOJ 1045 Digits of Factorial】
- day12总结
- Opencv Mat矩阵中data、size、depth、elemSize、step等属性的理解
- python 学习(十五)之 字典
- Spring AOP 实践(四)利用annotation实现声明式服务
- 暑假练习赛 007 A - Time
- MySQL 5.6.22 win32 zip版安装
- 不得不在脑海里徘徊的站外(第三方)SEO优化策略
- 字典树
- mysql中日期类型DATETIME和TIMESTAMP的区别