754. 平方矩阵 II

本以为是个打表题,没想到竟是个规律题orz

打表

const int N=110;
int a[N][N];
int n;

void init()
{
    for(int i=0;i<100;i++)
    {
        a[i][i]=1;
        for(int j=i+1;j<100;j++)
            a[i][j]=a[i][j-1]+1;
        for(int j=i-1;j>=0;j--)
            a[i][j]=a[i][j+1]+1;
    }
}

int main()
{
    init();

    while(cin>>n && n)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                cout<<a[i][j] <<' ';
            cout<<endl;
        }
        cout<<endl;
    }

    //system("pause");
    return 0;
}

找规律

int n;

int main()
{
    while(cin>>n && n)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                cout<<abs(i-j)+1<<' ';
            cout<<endl;
        }
        cout<<endl;
    }

    //system("pause");
    return 0;
}
posted @ 2021-01-24 09:33  Dazzling!  阅读(65)  评论(0编辑  收藏  举报