您好我正在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,因爲我是新與算法。
我確定*真實*愛因斯坦不會稱之爲「教師」:-)您或您的拼寫校正軟件正在尋找的詞是「階乘」。 –