Tim Buchholz
Tim Buchholz

Reputation: 83

pascal's triangle recursion

I need to write a Java-Code which prints the pascal's triangle. 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 (r-n) blanks. n stands for the current row and r is the total count of rows. 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

enter image description here

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

enter image description here

Upvotes: 3

Views: 2908

Answers (2)

Aziuth
Aziuth

Reputation: 3902

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.

More complex solution, that might not be better, depending on your taste:

Given n lines of numbers to be displayed

  1. Initialize a list positions_n, which stores the positions at which each number in the last line is to be displayed. In your example, the last line is

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)

  1. Iterating backwards, generate positions_i-1 from positions_i by taking the mean of two positions. For the example of positions_n, positions_n-1 would thus be (assuming we round up):

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.

Upvotes: 2

ben
ben

Reputation: 41

So you could format with "\t" but instead of using 1, use 2. Then you would be able to space the numbers every 1 space or every 2 spaces, allowing the first (top) one to be in between the second 2, and repeat.

Upvotes: 4

Related Questions