top1编程
← 返回题目
题解

【入门】有趣的数字图形I

1 条题解

  • 0
    @ 2026-7-31 9:33:32

    解题思路

    输出 N×N 方阵,只显示右上三角部分,对角线为 1,向右递增。

    参考代码

    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main() {
        int n; cin >> n;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                // 只有右上部分有数字,值从对角线向右递增
                if (j >= i) {
                    cout << setw(5) << (j - i + 1);
                } else {
                    cout << setw(5) << ' ';
                }
            }
            cout << endl;
        }
        return 0;
    }
    

    O(N²) 时间,O(1) 空间

    • 1