1 条题解

  • 0
    @ 2024-10-16 20:18:12

    参考答案:

    #include<iostream>
    using namespace std;
    const int N = 1010;
    int n;
    int a[N][N];
    int main() 
    {
        cin >> n;
        int i = 1, j = 1;
        bool flag = 1;
        for (int cnt = 1; cnt <= (n * n + 1) / 2; cnt++) 
        {
            a[i][j] = cnt;
            if (flag == 1) 
            {
                if (i == 1) 
                {
                    j++;
                    flag = 0;
                }
                else 
                {
                    i--;
                    j++;
                }
            }
            else 
            {
                if (j == 1) 
                {
                    i++;
                    flag = 1;
                }
                else 
                {
                    i++;
                    j--;
                }
            }
        }
        for (int i = 1; i <= n; i++) 
        {
            for (int j = 1; j <= n; j++) 
            {
                if (a[i][j] == 0) 
                {
                    a[i][j] = n * n + 1 - a[n - i + 1][n - j + 1];
                }
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
    }
    
    • 1

    信息

    ID
    5415
    时间
    1000ms
    内存
    256MiB
    难度
    1
    标签
    递交数
    2
    已通过
    1
    上传者