2016-11-02 50 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