简体   繁体   English

帕斯卡的三角递归

[英]pascal's triangle recursion

I need to write a Java-Code which prints the pascal's triangle. 我需要编写一个打印pascal三角形的Java代码。 This is what I did until now. 这就是我到现在为止所做的。

import java.util.Scanner;

class Pascal {

    static int bnk (int n, int k) {

        if (k==0 || k==n) {
            return 1;
        } // B(n,k) Berechnung für Standardwert = 1;
        else {
            int x = (bnk(n-1, k-1) + bnk(n-1, k));
            return x;
        } // Berechnung aller sonstigen B(n,k)-Werte. 
    } // Berechnung von B(n,k)

    public static void main (String [] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("How many rows?: ");
        int r = sc.nextInt();

        sc.close();

        for (int n=0; n<r; n++) {

            for (int j=0; j<(r-n); j++) {
                System.out.print(" "); 
            } // Setzt Anzahl Leerzeichen vor erster Zahl

            for (int k=0; k<=n; k++) {

                int b = bnk(n,k);
                System.out.print(b+" ");
            } // Berechnet in jeder Reihe alle Elemente

            System.out.println();
        } // Berechnet Reihe nach Reihe
    } // main
} // class Pascal

The first method calculates the values needed in the triangle. 第一种方法计算三角形中所需的值。 The second one prints the triangle row after row by printing (rn) blanks. 第二个通过打印(rn)空白打印行的三角形。 n stands for the current row and r is the total count of rows. n代表当前行,r代表行的总数。 So the left side of the triangle gets printed in the right way but the problem is, when the values in the triangle are getting too high, the triangle gets out of shape on the right side. 因此,三角形的左侧以正确的方式打印,但问题是,当三角形中的值变得太高时,三角形在右侧变形。 I hope what i described was understandable. 我希望我所描述的是可以理解的。 Could you help me find a way to properly format the triangle? 你能帮我找到一种正确格式化三角形的方法吗?

The current output looks like this: 当前输出如下所示:

5 rows: https://gyazo.com/d9a536d3ac92c155707ebb2e4ee7745b 5行: https//gyazo.com/d9a536d3ac92c155707ebb2e4ee7745b

在此输入图像描述

10 rows: https://gyazo.com/4ab0479f9324dd7c2911398ea5a71e33 10行: https//gyazo.com/4ab0479f9324dd7c2911398ea5a71e33

在此输入图像描述

所以你可以用"\\t"格式化,但不是使用1,而是使用2.然后你可以每1个空格或每2个空格分隔数字,允许第一个(顶部)位于第二个2之间,并重复。

Simple solution: 简单方案:

Compute the maximum of the digits that are to be displayed, in your case 3 for 126. Then, every number is displayed using this amount of space, with leading or trailing spaces to fill it up. 计算要显示的最大位数,在案例3中为126.然后,使用此空间量显示每个数字,并使用前导或尾随空格填充它。

More complex solution, that might not be better, depending on your taste: 更复杂的解决方案,可能不会更好,取决于您的口味:

Given n lines of numbers to be displayed 给出n行数字

  1. Initialize a list positions_n, which stores the positions at which each number in the last line is to be displayed. 初始化列表position_n,其存储将显示最后一行中的每个数字的位置。 In your example, the last line is 在您的示例中,最后一行是

1 9 36 84 126 126 84 36 9 1 1 9 36 84 126 126 84 36 9 1

Thus, positions_n would be set to 0, 2, 4, 7, 10, 14, 18, 21, 24, 26 (if I did no mistake) 因此,positions_n将被设置为0,2,4,7,10,14,18,21,24,26(如果我没有错误)

  1. Iterating backwards, generate positions_i-1 from positions_i by taking the mean of two positions. 向后迭代,通过取两个位置的平均值从positions_i生成position_i-1。 For the example of positions_n, positions_n-1 would thus be (assuming we round up): 对于position_n的例子,position_n-1因此(假设我们向上舍入):

1, 3, 6, 12, 16, 20, 23, 25 1,3,6,12,16,20,23,25

  1. Display the numbers at those positions. 显示这些位置的数字。

Problem with this is of course that we will have jumps that might look bad whenever we need to round. 问题在于,当我们需要进行回合时,我们的跳跃可能看起来很糟糕。 Also, the spacing between the numbers could get weird and uneven. 此外,数字之间的间距可能会变得奇怪和不均匀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM