【发布时间】:2017-11-05 04:44:08
【问题描述】:
因此,这样做的目标是使用三种方法生成帕斯卡三角形:一种生成下一行,主方法,以及(我遇到问题的那个)一种将数组移动到每行以创建一个三角形。我正在努力寻找能够做到这一点的算法。我已经提供了上下文的完整代码,但我真的只需要 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
我已经搜索了很多,但似乎找不到我要找的东西。
提前致谢, 约旦
【问题讨论】:
-
你所拥有的究竟有什么问题?
-
在上面添加了输出
标签: java pascals-triangle