递归方法打印 Pascal 的三角形倒置和右侧(通过布尔值)

Recursive method that prints Pascal's triangle upside-down and rightside-up (via boolean)

我正在尝试创建一种递归方法来打印帕斯卡三角形的上下颠倒和右侧向上。我想使用布尔变量在倒三角形和正三角形之间切换。至此,我已经成功编写了让帕斯卡三角形倒转的代码:

public class Main {
    boolean upsideDown = true;

    public void printPascal(int n) {
        if (!upsideDown) {
            //THIS IS WHERE I'M STUCK
        }
        if (upsideDown) {
            if (n < 0)
                return;
            for (int k = 0; k <= n; k++) {
                System.out.print(binom(n, k) + " ");
            }
            System.out.println();
            printPascal(n - 1);
        }
    }

    public int binom(int n, int k) {
        if (k == n || k == 0)
            return 1;
        else return binom(n - 1, k - 1) + binom(n - 1, k);
    }

    public static void main(String[] args) {
        new Main().printPascal(10); //This will print the Pascal's triangle
        //from levels 10 to 0
    }
}

所以现在,我正在尝试将 Pascal 的三角形打印为右侧向上(从 0 到 10 级)。我如何制作实现此目的的递归方法?

我知道网上有很多关于帕斯卡三角形的代码。但是我找不到任何专门针对我的问题的内容。

似乎最优雅的解决方案是更改打印顺序,以便先打印最后一步,然后是倒数第二步,等等,最后打印第一步。这行得通吗?

public void printPascal(int n) {
    if (!upsideDown) {
        if (n < 0)
            return;
        //print the future step first, then print current step
        printPascal(n - 1);
        System.out.println();
        for (int k = 0; k <= n; k++) {
            System.out.print(binom(n, k) + " ");
        }
    }
    if (upsideDown) {
        if (n < 0)
            return;
        for (int k = 0; k <= n; k++) {
            System.out.print(binom(n, k) + " ");
        }
        System.out.println();
        printPascal(n - 1);
    }
}

这是前两个答案的总结,在这里我们可以输入帕斯卡三角形中所需的必要行数。

public class Main {
    boolean upsideDown = true;
    public void printPascal(int n) {
        if (!upsideDown) {
            if (n < 0)
                return;
            printPascal(n - 1);
            System.out.println();
            for (int k = 0; k <= n; k++) {
                System.out.print(binom(n, k) + " ");
            }
        }
        if (upsideDown) {
            if (n < 0)
                return;
            for (int k = 0; k <= n; k++) {
                System.out.print(binom(n, k) + " ");
            }
            System.out.println();
            printPascal(n - 1);
        }
    }
    public int binom(int n, int k) {
        if (k == n || k == 0)
            return 1;
        else return binom(n - 1, k - 1) + binom(n - 1, k);
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = input.nextInt();
        new Main().printPascal(number - 1);
    }
}

你可以迭代创建一个带有帕斯卡三角形的二维数组,然后在左上角打印如下:

 1  1  1  1  1  1  1  1  1  1 
 1  2  3  4  5  6  7  8  9 
 1  3  6 10 15 21 28 36 
 1  4 10 20 35 56 84 
 1  5 15 35 70 126 
 1  6 21 56 126 
 1  7 28 84 
 1  8 36 
 1  9 
 1 

代码:

int n = 10;
// an array of 'n' rows
int[][] triangle = new int[n][];
// iterate over the rows of the array
for (int i = 0; i < n; i++) {
    // a row of 'n-i' elements
    triangle[i] = new int[n - i];
    // iterate over the elements of the row
    for (int j = 0; j < n - i; j++)
        if (i == 0 || j == 0)
            // elements of the first row
            // and column are equal to one
            triangle[i][j] = 1;
        else
            // all other elements are the sum of the
            // previous element in the row and column
            triangle[i][j] = triangle[i][j - 1] + triangle[i - 1][j];
}
// output
for (int[] row : triangle) {
    for (int element : row)
        // format as a two-digit number
        System.out.printf("%2d ", element);
    System.out.println();
}

另请参阅:Pascal's triangle 2d array - formatting printed output