E. Start of the season

链接

[https://codeforces.com/problemset/problem/12/E]

题意

构造一个n阶矩阵使得它对此且同一行每个数字都不一样。对角线为0

分析

暴露思维的缺陷,其实如果没有对角线为0的要求我是知道直接下一行与上一行错一位即可。
必须学会用自己知道的东西去解决未知的东西。很显然这方面我真的辣鸡死了。只能说思维不够牛逼
回道题目。我们先不考虑0这些位置。
先想一个n-1阶不包含0的,那么就是错位即可。就是每次第一个往最后一个挪。
比如 n=4
3阶
是这样的
1 2 3
2 3 1
3 1 2
那这就满足题意了,但是没有0啊。
1 2 3 0
2 3 1 0
3 1 2 0
0 0 0 0
这时候只需要0变为相应行和列的值,对角线变为0即可
0 2 3 1
2 0 1 3
3 1 0 2
1 3 2 0

代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int a[N][N];
int main(){
    int n; string s;
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    while(cin>>n)
    {
        n-=1;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        a[i][j]=(i+j)%n+1;
        for(int i=0;i<n;i++)
        a[i][n]=a[i][i],a[n][i]=a[i][i],a[i][i]=0;
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            cout<<a[i][j]<<' ';
            cout<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mch5201314/p/10907766.html