d****2 发帖数: 3 | 1 一锅汤,有胡萝卜和汤,各50份。然后有一堆人会来接汤,有四种人
a. 接一份里面有75%的胡萝卜,25%的汤
b. 接一份里面有50%的胡萝卜,50%的汤
c. 接一份里面有25%的胡萝卜,75%的汤
d. 接一份里面有0%的胡萝卜,100%的汤
请问 当没有汤的时候,还剩下胡萝卜的这种情况的概率。 | a*******y 发帖数: 15 | 2 import java.util.*;
class CarrotSoup{
static final int[][] d=new int[][]{{-4,0},{-3,-1},{-2,-2},{-1,-3}};
public static void main(String[] s){
int n=50*4;
double[][] state=new double[n+1][n+1];
state[n][n]=1;
for(int t=0; t
int sum=2*n-t*4, carrot;
for(int soup=Math.max(1,sum-n); soup<=n && (carrot=sum-soup)>=0;
soup++){
// when soup=0 or carrot<0 stop transfer the state
int count=0;
for(int i=0; i<4; i++)
if(soup+d[i][0]>=0 && carrot+d[i][1]>=0)
count++;
for(int i=0; i<4; i++)
if(soup+d[i][0]>=0 && carrot+d[i][1]>=0)
state[soup+d[i][0]][carrot+d[i][1]]+=state[soup][
carrot]/count;
state[soup][carrot]=0;
}
}
double soupEmpty=0, soupEmptyWithCarrot=0;
for(int carrot=1; carrot<=n; carrot++)
soupEmptyWithCarrot+=state[0][carrot];
soupEmpty=soupEmptyWithCarrot+state[0][0];
System.out.println(soupEmptyWithCarrot+" / "+soupEmpty);
System.out.println(soupEmptyWithCarrot/soupEmpty);
}
} | r*********r 发帖数: 53 | |
|