CF-12E - Start of the season(思维)

E - Start of the season

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

<p< p="">

The first line contains one integer n (2 ≤ n ≤ 1000), n is even.

Output

<p< p="">

Output n lines with n numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any.

Sample Input

Input
2
Output
0 1
1 0
Input
4
Output
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";
      }
  }
}


 

 

 

 

转载于:https://www.cnblogs.com/nealgavin/archive/2013/01/11/3206192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值