编辑代码

#include <iostream>

using namespace std;

// 递归算法
void recursivePrint(int n) {
    if (n > 0) {
        recursivePrint(n - 1);
        for (int i = 0; i < n; ++i) {
            cout << n << " ";
        }
        cout << endl;
    }
}

// 非递归算法
void iterativePrint(int n) {
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < i; ++j) {
            cout << i << " ";
        }
        cout << endl;
    }
}

int main() {
    int n;
    cout << "Enter the number of rows: ";
    cin >> n;

    cout << "Recursive Output:" << endl;
    recursivePrint(n);

    cout << "Iterative Output:" << endl;
    iterativePrint(n);

    return 0;
}