Crawling in process...Crawling failedTime Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix.
Input
The first line contains one integer n (2 ≤ n ≤ 1000), n is even.
Output
Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any.
Sample Input
2
0 1 1 0
4
0 1 3 2 1 0 2 3 3 2 0 1 2 3 1 0
思路:每次错1位可以保证每一行都不同。而对称怎么解决呢?先不管0
弄成n-1维其符合题目条件的就很容易做到了。看例子:
输入为6
则不管0 错1位构造为
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
符合题目中的意思吧。现在吧0放到对角线上,并且把对角线上的数加到两边仍然符合要求。到这答案就出来了。
1 2 3 4 5 0 0 2 3 4 5 1
2 3 4 5 1 0 2 0 4 5 1 3
3 4 5 1 2 0 3 4 0 1 2 5
4 5 1 2 3 0 -> 4 5 1 0 3 2
5 1 2 3 4 0 5 1 2 3 0 4
0 0 0 0 0 0 1 3 5 2 4 0
1 2 3 4 5 0 0 2 3 4 5 1
2 3 4 5 1 0 2 0 4 5 1 3
3 4 5 1 2 0 3 4 0 1 2 5
4 5 1 2 3 0 -> 4 5 1 0 3 2
5 1 2 3 4 0 5 1 2 3 0 4
0 0 0 0 0 0 1 3 5 2 4 01 2 3 4 5 0 0 2 3 4 5 1
2 3 4 5 1 0 2 0 4 5 1 3
3 4 5 1 2 0 3 4 0 1 2 5
4 5 1 2 3 0 -> 4 5 1 0 3 2
5 1 2 3 4 0 5 1 2 3 0 4
0 0 0 0 0 0 1 3 5 2 4 01 2 3 4 5 0 0 2 3 4 5 1
2 3 4 5 1 0 2 0 4 5 1 3
3 4 5 1 2 0 3 4 0 1 2 5
4 5 1 2 3 0 -> 4 5 1 0 3 2
5 1 2 3 4 0 5 1 2 3 0 4
0 0 0 0 0 0 1 3 5 2 4 0
#include<iostream>
#include<cstring>
using namespace std;
const int mm=1007;
int map[mm][mm];
int n;
int main()
{
while(cin>>n)
{ n--;
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
map[i][j]=(i+j)%n+1;
for(int i=0;i<n;i++)
map[i][n]=map[n][i]=map[i][i],map[i][i]=0;
for(int i=0;i<=n;i++)
{cout<<map[i][0];
for(int j=1;j<=n;j++)
{
cout<<" "<<map[i][j];
}
cout<<"\n";
}
}
}
在足球赛季开始之前,需要构建一个特殊的n×n大小的魔法矩阵,该矩阵需包含从0到n-1的不同整数,且主对角线全为0,同时矩阵还要保持对称性。本文介绍了一种通过编程实现这一复杂任务的方法。
453

被折叠的 条评论
为什么被折叠?



