Java - 创建一个采用数组并将线移过来的方法,创建一个三角形(Pascal Triangle)

时间:2017-11-05 04:44:09

标签: java pascals-triangle

所以这样做的目的是使用三种方法生成一个pascal三角形:一个生成下一行,一个是主方法,一个是我遇到麻烦的一个方法,一个将每个数组移过一行来创建一个三角形。我正在努力寻找能够做到这一点的算法。我已经为上下文提供了完整的代码,但我真的只是在寻求printRow函数的帮助。

所以这是:

package pascaltrianglegenerator;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class PascalTriangleGenerator {

    public static void main(String[] args) throws IOException {

    Scanner cin = new Scanner(System.in);

        System.out.print("Enter the name of the output file -> ");
        PrintWriter fileOut = new PrintWriter(new File(cin.next()));

        System.out.print("Enter the number of rows for the triangle -> ");
        int numberRows = cin.nextInt();

        if(numberRows < 0) {
            System.out.println("ERROR: The number of rows must be a positive integer.");
        } else {

            int[] currentRow = {};
            for(int i = 1; i <= numberRows; i++) {

                currentRow = nextRow(currentRow);
                printRow(currentRow, numberRows, (numberRows - i), fileOut);

            }
            fileOut.close();
        }
    }

    /**
    * Computes an array containing the coefficients of the binomial (x+y)^(k+1)
    * given an array containing the coefficients of the binomial (x + y)^k.
    * @param currentRow an array that contains the coefficients of an expansion
    * of the binomial (x + y)^k for some k greater than or equal to 0.
    * @return an array containing the binomial coefficients of an expansion of
    * the binomial (x + y)^(k+1) or the array [1] when currentRow is null.
    */
    public static int[] nextRow(int[] currentRow) {

        int[] nextRow;
        if(currentRow.length == 0) {
            nextRow = new int[1];
            nextRow[0] = 1;
        } else {
            nextRow = new int[currentRow.length + 1];
            for(int i = 1; i < nextRow.length/2; i++) {
                nextRow[i] = currentRow[i] + currentRow[i-1];
            }
            int counter;
            if(nextRow.length % 2 == 0) {
                counter = 1;
                for(int i = nextRow.length/2; i <nextRow.length - 1; i++) {
                    nextRow[i] = nextRow[i - counter];
                    counter += 2;
                }
            } else {
                nextRow[nextRow.length/2] = currentRow[nextRow.length/2] * 2;
                counter = 2;
                for(int i = nextRow.length/2 + 1; i < nextRow.length - 1; i++) {
                    nextRow[i] = nextRow[i - counter];
                    counter += 2;
                }
            }
            nextRow[0] = 1;
            nextRow[nextRow.length - 1] = 1;
        }
        return nextRow;
    }

我已经尝试了很多方程式,但似乎无法做到正确。

    /**
    * Displays a row of the Pascal’s triangle
    * @param row an array containing binomial coefficients
    * @param numRows the number of rows that the triangle will have
    * @param shift the number of columns by which the first coefficient, 1, of 
    * the next row is shifted leftward from the first coefficient on the previous row.
    * @param writer a reference to a file output stream
    */
    public static void printRow(int row[], int numRows, int shift, PrintWriter writer) { 

        String formatString = "";
        for (int i = 1; i <= numRows; i++) {
            formatString += row[i] + " ";
        }
        System.out.printf("%" + (shift) + "s%n", formatString);
        writer.printf(formatString);
    }
}

这是输入12行时代码返回的内容:

         1 
      1 1 
   1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 
Exception in thread "main" java.util.FormatFlagsConversionMismatchException: 
Conversion = s, Flags = 0

我搜索了很多,似乎无法找到我正在寻找的东西。

提前致谢, 约旦

1 个答案:

答案 0 :(得分:0)

您可以迭代移位并打印出空格,这可以解决您的崩溃问题:

public static void printRow(int row[], int numRows, int shift, PrintWriter writer) { 

    String formatString = "";
    for (int i = 0; i < row.length; i++) { // firstly, should be i = 0 and row.length is used here ...
        formatString += row[i] + " ";
    }
    // start a new line:
    System.out.println();

    // print out all the space chars for the shift:
    for (int j = 0; j < shift; j++) {
        System.out.print(" ");
    }

    System.out.print(formatString);
    writer.printf(formatString);
}

现在你只需要稍微处理格式化,这是新的输出:

       1 
      1 1 
     1 2 1 
    1 3 3 1 
   1 4 6 4 1 
  1 5 10 10 5 1 
 1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 

1 8 28 56 70 56 28 8 1   1 9 36 84 126 126 84 36 9 1  1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1

所以双位和三位数确实影响格式,但我希望这有点帮助:)