所以我目前正在完成一项我似乎无法完成的任务。好吧,我已经完成了一切,但是想要额外的功劳。我一直在寻找网络,似乎无法找到我正在寻找的确切内容。
public class PascalTester
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to the Pascal's Triangle program!");
System.out.println("Please enter the size of the triangle you want");
int size = kb.nextInt();
int[][] myArray = new int[size][size];
myArray = fillArray(myArray);
//myArray = calculateArray(myArray);
printArray(myArray); //prints the array
}
private static int[][] fillArray(int[][] array)
{
array[0][1] = 1;
for (int i = 1; i < array.length; i++)
{
for (int j = 1; j < array[i].length; j++)
{
array[i][j] = array[i-1][j-1] + array[i-1][j];
}
}
return array;
}
private static void printArray(int[][] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if(array[i][j] != 0)
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
我现在唯一的问题是将输出正确格式化为实际三角形。任何建议在这个时候都会非常有用。提前致谢
答案 0 :(得分:1)
对此的一种方法是,假设您将所有数字格式化为相同宽度,将问题视为以线为中心的问题。
Java编码留给读者,但基本上是:
for lineText : triange lines
leadingSpacesCount = (80/2) - lineText.length();
print " " x leadingSpacesCount + lineText
答案 1 :(得分:0)
尝试使用http://www.kodejava.org/examples/16.html处的技术创建一个带array.length - i - 1
个空格的数组(需要在数字之间添加数字空格..和2个数字的数字,如果有的话......)。< / p>
在外部for循环的开头打印此数组。
答案 2 :(得分:0)
这里的挑战是你想要在三角形的顶部开始打印,但是在到达三角形的最后一行(也是最宽的一行)之前,你不知道每行的中心位置。诀窍是在你知道最后一行的宽度之前不要打印任何东西。一种方法是将所有行生成为String
(或StringBuilder
)个对象并计算最大宽度。然后,从顶部开始,通过首先打印适当数量的空格使每条线居中。正确的空格数是
(maxLineLength - currentLine.length()) / 2
或者,您可以简单地假设最大线长并使该宽度中的所有线居中。如果较长的线超过最大宽度,则三角形将在某一行下方失真。 (请务必不要尝试打印负数空格!)
答案 3 :(得分:0)
如果有人正在寻找实际的代码来看看我在Java中的实现,它与Craig Taylor提到的相似(数字格式化为相同的宽度)加上它使用算法来计算没有记忆(或阶乘)的元素。
代码包含解释每个步骤(计算和打印)的注释:
/**
* This method will print the first # levels of the Pascal's triangle. It
* uses the method described in:
*
* https://en.wikipedia.org/wiki/Pascal%27s_triangle#Calculating_a_row_or_diagonal_by_itself
*
* It basically computes the Combinations of the current row and col
* multiplied by the previous one (which will always be 1 at the beginning
* of each pascal triangle row). It will print each tree element to the output
* stream, aligning the numbers with spaces to form a perfect triangle.
*
* @param num
* # of levels to print
*/
public static void printPascalTriangle(int num) {
// Create a pad (# of spaces) to display between numbers to keep things
// in order. This should be bigger than the # of digits of the highest
// expected number and it should be an odd number (to have the same
// number of spaces to the left and to the right between numbers)
int pad = 7;
// Calculate the # of spaces to the left of each number plus itself
// (this is the width of the steps of the triangle)
int stepsWidth = pad / 2 + 1;
// Now calculate the maximum # of spaces from the left side of the
// screen to the first triangle's level (we will have num-1 steps in the
// triangle)
int spaces = (num - 1) * stepsWidth;
for (int n = 0; n < num; n++) {
// Print the left spaces of the current level, deduct the size of a
// number in each row
if (spaces > 0) {
System.out.printf("%" + spaces + "s", "");
spaces -= stepsWidth;
}
// This will represent the previous combination C(n k-1)
int prevCombination = 1;
for (int k = 1; k <= n + 1; k++) {
System.out.print(prevCombination);
// Calculate how many digits this number has and deduct that to
// the pad between numbers to keep everything aligned
int digits = (int) Math.log10(prevCombination);
if (digits < pad) {
System.out.printf("%" + (pad - digits) + "s", "");
}
// Formula from Wikipedia (we can remove that "+1" if we start
// the row loop at n=1)
prevCombination = prevCombination * (n + 1 - k) / k;
}
// Row separator
System.out.println();
}
}
希望它有所帮助!