您好我正在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,因为我是新与算法。
我确定*真实*爱因斯坦不会称之为“教师”:-)您或您的拼写校正软件正在寻找的词是“阶乘”。 –