【发布时间】:2019-12-22 16:14:30
【问题描述】:
我一直在尝试使用组合公式编写帕斯卡三角形,但它无法正常工作,我不确定问题出在哪里?
这是输入:
public class Probs {
public static int fact(int n) {
int f;
for (f = 1; n > 1; n--) {
f *= n;
}
return f;
}
public static int comb(int i, int j) {
return fact(i) / fact(i - j) * fact(j);
}
public static void main(String[] args) {
int n = 5;
int i;
int j;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i; j++) {
System.out.print(" ");
}
for (j = 0; j <= i; j++) {
System.out.print(" " + comb(i, j));
}
System.out.println();
}
}
}
输出:
1
1 1
1 2 4
1 3 12 36
1 4 24 144 576
你能以一种对初学者友好的方式解释我为什么吗?
【问题讨论】:
标签: java algorithm recursion pascals-triangle