所以我目前的工作,我只是似乎无法完成的任务。 嗯,我所拥有的一切做完,但想额外的信用。 我一直在寻找各地的网络,并不能真正似乎找到正是我要找的。
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();
}
}
}
那我现在遇到的唯一问题是正确格式化输出看起来像一个实际的三角形。 任何建议将是在这个时间点非常有帮助。 提前致谢
一种方法来此,是,假设你已经格式化为相同的宽度所有数字,是把问题为中心的线路。
Java编码留作练习读者但本质:
for lineText : triange lines
leadingSpacesCount = (80/2) - lineText.length();
print " " x leadingSpacesCount + lineText
尝试使用该技术在http://www.kodejava.org/examples/16.html使阵列与array.length - i - 1
空间(需要数字之间添加的号码空间..和2号的2数字如果任何数字。)。
在外部for循环的开始打印当前阵列。
这里的挑战是,你要开始在三角形的顶部印,但你不知道从哪里居中每一行,直到你到达三角形的最后一个(最宽的)一行。 诀窍是没有打印任何东西,直到你知道最后一行有多宽。 做到这一点的一种方法是产生所有的行作为String
(或StringBuilder
)对象,并计算最大宽度。 然后,从顶部,通过第一印版的空间适当数量的中心中的每个线。 空格正确的号码将是
(maxLineLength - currentLine.length()) / 2
或者,可以简单地假设的最大行长度和中心在该宽度的行。 如果更长的线超过最大宽度,则该三角形将会失真低于某一行。 (只要确保不尝试打印的空间为负数!)
如果有人正在寻找实际的代码来做到这一点看看我的Java实现,它类似于克雷格·泰勒提到的(数字格式为相同的宽度),再加上它使用一种算法来计算无记忆的元素(或阶乘) 。
代码有注释解释每个步骤(计算和印刷):
/**
* 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();
}
}
希望它可以帮助别人!