【发布时间】:2009-11-05 06:27:01
【问题描述】:
... 最好是在 Java 中。这是我所拥有的:
//x choose y
public static double choose(int x, int y) {
if (y < 0 || y > x) return 0;
if (y == 0 || y == x) return 1;
double answer = 1;
for (int i = x-y+1; i <= x; i++) {
answer = answer * i;
}
for (int j = y; j > 1; j--) {
answer = answer / j;
}
return answer;
}
我想知道是否有更好的方法来做到这一点?
【问题讨论】:
-
由于选择函数被定义为自然数上的二进制函数 (|N x |N => |N),因此在任何时候都不应使用双精度数。这实际上会使您选择的解决方案不必要地处于次优状态。
标签: java combinations binomial-coefficients