2013-01-17 67 views
0

所以我目前正在完成一项任务,我似乎无法完成。那么我已经完成了一切,但希望额外的功劳。我一直在寻找网络,并不能真正找到我正在寻找的东西。pascal三角形正确格式化java

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(); 
    } 

} 

} 

我现在唯一的问题是正确地格式化输出看起来像一个实际的三角形。任何建议在此时都会非常有用。在此先感谢

+1

什么是当前输出?你想让它看起来像什么?你有什么尝试? –

+0

它是一个直角三角atm,我期待做一个等腰三角形。我试图搞乱printf格式,但是当我在编译器中出现错误 – kevorski

+0

你应该展示你试过的东西,毕竟,如果我们这样做,你应该得到额外的功劳吗? –

回答

0

尝试使用http://www.kodejava.org/examples/16.html技术来创建一个数组与array.length - i - 1空格(需要添加数字之间的数字空格..和2个数字的2位数字,如果有..)。

在outer for循环的开始位置打印此数组。

1

对此的一种方法是,假设您将所有数字格式化为相同的宽度,则将该问题视为居中行的问题。

Java编码留给练习读者但本质:

for lineText : triange lines 
    leadingSpacesCount = (80/2) - lineText.length(); 
    print " " x leadingSpacesCount + lineText 
0

这里的挑战是,你要开始在三角形的顶部印,但你不知道从哪里居中每一行,直到你到达三角形的最后(最宽)行。诀窍是在你知道最后一行有多宽之前不要打印任何东西。一种方法是将所有行生成为String(或StringBuilder)对象并计算最大宽度。然后,从顶部开始,通过首先打印适当数量的空格对齐每一行。的空间的正确数量将是

(maxLineLength - currentLine.length())/2 

或者,可以简单地假设的最大行长度和中心在该宽度的行。如果较长的线条超过最大宽度,那么三角形将在某一行下方变形。 (请务必不要尝试打印负数的空格!)

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(); 
    } 
} 

希望它可以帮助别人!

+0

即使链接可能包含答案,请在此发布您的相关代码。 – bish

+0

好电话bish。代码发布而不是链接。 – JavierJ