33,027
社区成员




public class Problem {
public static void problem(boolean flag, int times) {
int count = 0;
for (int i = 0; i < times; i++) {
int[] boxs = new int[3];//三个盒子
int index = (int) (Math.random() * 3);//有奖的盒子所在
boxs[index] = 1;
int choose = (int) (Math.random() * 3);//初次选择
//打开剩下两个盒子中的空盒子
int reflct=-1;
for (int k = 0; k < 2; k++) {
if(k!=choose&&boxs[k]==0){
reflct=k;
break;
}
}
if (flag) {
for (int j = 0; j < 2; j++) {
if (j != choose && j != reflct) {
if (boxs[j] == 1) {
count++;
}
}
}
} else {
if (boxs[choose] == 1) {
count++;
}
}
}
if(flag){
System.out.println("若选择换,"+times+"次试验得选中概率为"+(count*1.0/times));
}else{
System.out.println("若选择不换,"+times+"次试验得选中概率为"+(count*1.0/times));
}
}
public static void main(String[] args) {
problem(true, 5000);
problem(false, 5000);
}
}
public class Problem {
public static void problem(boolean flag, int times) {
int count = 0;
for (int i = 0; i < times; i++) {
int[] boxs = new int[3];//三个盒子
int index = (int) (Math.random() * 3);//有奖的盒子所在
boxs[index] = 1;
int choose = (int) (Math.random() * 3);//初次选择
//打开剩下两个盒子中的空盒子
int reflct = (int) (Math.random() * 3);
while (reflct == choose || boxs[reflct] == 1) {
reflct = (int) (Math.random() * 3);
}
if (flag) {
for (int j = 0; j < 2; j++) {
if (j != choose && j != reflct) {
if (boxs[j] == 1) {
count++;
}
}
}
} else {
if (boxs[choose] == 1) {
count++;
}
}
}
if(flag){
System.out.println("若选择换,"+times+"次试验得选中概率为"+(count*1.0/times));
}else{
System.out.println("若选择不换,"+times+"次试验得选中概率为"+(count*1.0/times));
}
}
public static void main(String[] args) {
problem(true, 5000);
problem(false, 5000);
}
}