static double[][] out = new double[2001][2001];
public static double help(int a, int b) {
if (out[a][b] != -1) {
return out[a][b];
}
double re = 0;
if (a <= b) {
out[a][b] = 0;
return 0;
} else if (b == 0) {
out[a][b] = 1;
return 1;
}
re = a * 1.0 / (a + b) * help(a - 1, b) + b * 1.0 / (a + b) * help(a, b - 1);
out[a][b] = re;
return re;
}
public static void main(String[] args) {
for (int i = 0; i < 2001; i++) {
for (int j = 0; j < 2001; j++) {
out[i][j] = -1;
}
}
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int a = in.nextInt();
int b = in.nextInt();
double re = help(a, b);
System.out.printf("Case #" + (t + 1) + ": %.8f\n", re);
}
}
用数组保存概率计算结果,避免重复计算。
Kickstart Practice Round 2017 Problem B. Vote
最新推荐文章于 2020-09-14 19:09:44 发布