题解:
- 先对n进行优化,除以25 上取整
分析A和B汤
A | B | 概率 |
---|---|---|
<=0 | <=0 | 0.5 |
>0 | <=0 | 0 |
<=0 | >0 | 1 |
>0 | >0 | f[i][j] = (f[i-4][j] + f[i-3][j-1] + f[i-2][j-2] + f[i-3][j-1]) / 4 |
- 当n很大的时候,A先分完的概率则越逼近1
class Solution {
public double soupServings(int n) {
n = (n + 24) / 25;
if (n >= 500) return 1;
double[][] f = new double[n + 1][];
for (int i = 0; i <= n; i++) {
f[i] = new double[n + 1];
for (int j = 0; j <= n; j++) {
if (i <= 0 && j <= 0) f[i][j] = 0.5;
else if (i > 0 && j <= 0) f[i][j] = 0;
else if (i <= 0 && j > 0) f[i][j] = 1;
else {
f[i][j] = (f[g(i - 4)][j] + f[g(i - 3)][g(j - 1)] +
f[g(i - 2)][g(j - 2)] + f[g(i - 1)][g(j - 3)]) / 4;
}
}
}
return f[n][n];
}
int g(int x) {
return Math.max(0, x);
}
}
标签:25,return,int,double,808,分汤
From: https://www.cnblogs.com/eiffelzero/p/16913745.html