Java - 创建一个方法,它接受一个数组并将线移动过来,创建一个三角形 (Pascal Triangle)
Java - Creating a method that takes an array and shifts the line over, creating a triangle shape (Pascal Triangle)
因此,这样做的目标是使用三种方法生成帕斯卡三角形:一种生成下一行,即主要方法,以及(我遇到麻烦的一种)将数组逐行移动的方法创建一个三角形。我正在努力寻找一种算法来做到这一点。我已经提供了上下文的完整代码,但我真的只需要 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行时的代码returns:
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
我搜索了很多,似乎找不到我要找的东西。
提前致谢,
乔丹
您可以迭代 shift 并打印出空格,这可以修复您的崩溃:
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
所以两位数和三位数确实会影响格式,但我希望这能有所帮助:)
因此,这样做的目标是使用三种方法生成帕斯卡三角形:一种生成下一行,即主要方法,以及(我遇到麻烦的一种)将数组逐行移动的方法创建一个三角形。我正在努力寻找一种算法来做到这一点。我已经提供了上下文的完整代码,但我真的只需要 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行时的代码returns:
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
我搜索了很多,似乎找不到我要找的东西。
提前致谢, 乔丹
您可以迭代 shift 并打印出空格,这可以修复您的崩溃:
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
所以两位数和三位数确实会影响格式,但我希望这能有所帮助:)