1 条题解

  • 0
    @ 2024-7-30 17:39:45
    #include <bits/stdc++.h>
    using namespace std;
    vector<vector<int>> ovo(int n) {
        vector<vector<int>> matrix(n, vector<int>(n));
        int num = 1;
        int left = 0, right = n - 1, top = 0, bottom = n - 1;
    
        while (left <= right && top <= bottom) {
            for (int i = left; i <= right; ++i) {
                matrix[top][i] = num++;
            }
            top++;
            for (int i = top; i <= bottom; ++i) {
                matrix[i][right] = num++;
            }
            right--;
            if (top <= bottom) {
                for (int i = right; i >= left; --i) {
                    matrix[bottom][i] = num++;
                }
                bottom--;
            }
            if (left <= right) {
                for (int i = bottom; i >= top; --i) {
                    matrix[i][left] = num++;
                }
                left++;
            }
        }
    
        return matrix;
    }
    int main() {
        int n;
        cin >> n;
        vector<vector<int>> matrix = ovo(n);
        for (const auto& row : matrix) {
            for (int num : row) {
                cout << num << " ";
            }
            cout << endl;
        }
        return 0;
    }
    

    不用 vector 的只能度过一个相对失败的人生(

    信息

    ID
    491
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    85
    已通过
    30
    上传者