2016-11-02 47 views
2

您好我正在Java中制作迭代帕斯卡三角形。到目前为止,一切都很好,直到行数超过13为止。输出变得有缺陷。我一定在这里做错了,请帮忙。帕斯卡的三角形错误输出

IterativePascal:

public class IterativePascal extends ErrorPascal implements Pascal { 
    private int n; 
    IterativePascal(int n) throws Exception { 
     super(n); 
     this.n = n; 
    } 
    public void printPascal() { 
     printPascal(false); 
    } 

    public void printPascal(boolean upsideDown) { 
     if (n == 0) { return; } 
     for (int j = 0; j <= n; j++) { 
      for (int i = 0; i < j; i++) { 
       System.out.print(binom(j - 1, i) + (j == i + 1 ? "\n" : " ")); 
      } 
     } 
    } 

    public long binom(int n, int k) { 
     return (k == 0 || n == k) ? 1 : faculty(n)/(faculty(k) * faculty(n - k)); 
    } 

    private long faculty(int n) { 
     if (n == 0 || n == 1) { return 1; } 
     int result = 1; 
     for (int i = 2; i <= n; i++) { 
      result = result * i; 
     } 
     return result; 
    } 
} 

输出:

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 
1 12 66 220 495 792 924 792 495 220 66 12 1 
1 4 24 88 221 399 532 532 399 221 88 24 4 1 <----- wrong 
1 0 1 5 14 29 44 50 44 29 14 5 1 0 1 <----- wrong 

帮助将appriciated,因为我是新与算法。

+1

我确定*真实*爱因斯坦不会称之为“教师”:-)您或您的拼写校正软件正在寻找的词是“阶乘”。 –

回答

6

你到达数字溢出。因为14!太大而无法填写java long

解决方案将使用+而不是!

将您的三角形保持为二维数组并遍历它。每个单元格应该是两个'以上'的总和。

+---+---+---+---+ 
| 1 | | | | 
| 1 | 1 | | | 
| 1 | 2 | 1 | | 
| 1 | 3 | 3 | 1 | 
+---+---+---+---+ 

的代码将是因为它遵循:

public static void triangle(int n) { 
    int[][] triangle = new int[n]; 
    for (int i = 0; i < n; i++) { 
     triangle[i] = new int[i+1]; 
    } 
    triangle[0][0] = 1; 
    triangle[1][0] = 1; 
    triangle[1][1] = 1; 
    for (int i = 2; i < n; i++) { 
     triangle[i][0] = 1; 
     for (int j = 1; j < triangle[i].length - 1; j++) { 
      triangle[i][j] = triangle[i-1][j] + triangle[i-1][j+1]; 
     } 
     triangle[i][triangle[i].length-1] = 1; 
    } 
    printArray(triangle); 
} 

编辑:
由于OP需要与binoms递归解决方案,我决定将水导入BigInteger S作为它可能是案件。

public BigInteger binom(int n, int k) { 
     return (k == 0 || n == k) ? BigInteger.ONE : faculty(n).divide((faculty(k).multiple(faculty(n - k))); 
    } 

private BigInteger faculty(int n) { 
    BigInteger result = BigInteger.ONE; 
    while (!n.equals(BigInteger.ZERO)) { 
     result = result.multiply(n); 
     n = n.subtract(BigInteger.ONE); 
    } 
    return result; 
} 

public void printPascal(boolean upsideDown) { 
    if (n == 0) { return; } 
    for (int j = 0; j <= n; j++) { 
     for (int i = 0; i < j; i++) { 
      System.out.print(binom(j - 1, i).toString() + (j == i + 1 ? "\n" : " ")); 
     } 
    } 
} 
+0

我有一个递归的方法,也包含长binom,它工作正常。我真的需要坚持我的原始代码。谢谢您的帮助。 – Einstein

+0

如果你想使用binom,只需从longs切换到BigInteger。记住,你可以更有效地计算binom。我的意思是,你不应该执行任何分裂。而不是计算'n!/(k!(nk)!'计算n *(n-1)* ... *(k + 1)/(nk)!'仍然比您的代码更好 – xenteros

0

也许问题出在你的阶乘的计算:我假设你的int类型可容纳人数达到32位,但13!比那更大。

您可以检查long是否可以存储更大的数字并定义结果。

+0

优秀的接受答案已经存在,在回答之前尽量确保你有新的信息(这个答案目前处于低质量队列中) – Zong

+2

也许你应该真的确信在写作之前哪个答案是第一个 – asp

+0

你是对的,对不起,我没有注意到另一个答案也是最近的,对于一个新用户来说,回答老问题很常见, – Zong