编辑代码

public class RecursivePattern {
    // 递归方式
    public static void printPatternRecursive(int n) {
        if (n > 0) {
            printPatternRecursive(n - 1);  // 递归调用
            for (int i = 0; i < n; i++) {
                System.out.print(n + " ");
            }
            System.out.println();
        }
    }
    // 非递归方式
    public static void printPattern(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int num = 6;
        System.out.println("递归算法:");
        printPatternRecursive(num);

        System.out.println();
        
        System.out.println("非递归算法:");
        printPattern(num);
    }
}