//201109212123
/*
* 有三扇门,其中一扇门后面是一辆轿车,另两扇门后面各有一只羊.给你一次猜的机会.
* 猜中羊可以牵走羊,猜中车可以开走车.当然大家都希望能开走汽车.
* 现在假如你猜1号门后面是车,然后主持人把无车的一扇门(比如2号门)打开.
* 现在再给你一次机会,请问你是否要换3号门?
*/
import java.util.*;
public class probability {
public static void main(String[] args) {
Random abc = new Random();
int[] door = new int[3];
double probability = 0.0;
int total = 0;
int num=0;
int total2 = 0;
int change = 0;
double probability2 = 0.0;
for(int j=0; j<1000; j++) {
num++;
for(int i=0; i<3; i++)
door[i] = 0;
int car = abc.nextInt(3);
door[car] = 1;
int choose = abc.nextInt(3);
if(door[choose] == 1)
total2 += 1;
int nocar;
for(int i=0; i<1000; i++) {
nocar = abc.nextInt(3);
if(nocar != choose && door[nocar] != 1) {
for(change=0; change<3; change++) {
if(change != choose && change != nocar) {
if(door[change] == 1)
total += 1;
break;
}
}
break;
}
}
//System.out.println("car:"+car+" chose:"+chose+" change:"+change);
}
probability = (double)total/num;
System.out.println(probability);
probability2 = (double)total2/num;
System.out.println(probability2);
System.out.println("num:"+num+ " total:"+total+" total2:"+total2);
}
}
Java 三扇门
最新推荐文章于 2025-08-06 16:23:50 发布