用编程去解决概率论问题

题目:智力电视节目里有三扇门,其中两扇门背后各是一只羊,还有一扇背后是一辆汽车。参赛者先选择一扇门,然后主持人打开剩下两扇其中一扇背后必定是羊的门。这时参赛者跟换选项是否得到汽车的概率更大呢?如果更换了概率是多少?

这道题算是很经典的博弈论了,网上一搜一大把,但是好像没有从编程角度出发去解决这个问题的,所以身为程序员的我,怎么能不用程序去跑一边呢?于是乎,代码就出来了。(为了统计的准确性,我们将次数放大到100000次,这样得到的数据更加准确)

public class Test {
    public static void main(String[] str) {
        int j = 0;//累计主持人筛选后剩下的那扇门是车的次数
        Random random = new Random();
        for (int i = 0; i < 100000; i++) {
            int car = random.nextInt(3);//在三个数据中随机生成一辆车(0,1,2)
            int choose = random.nextInt(3);//嘉宾从(0,1,2)中选择
            if (car == choose) {//如果选择是正确的,那么被主持人筛出后剩下的那扇门几率为0,肯定不为车,所以此处不用累加j

            } else {//假如选择是错误的,那么车肯定被主持人给筛选剩下了,那么必为车,则j累加
                j++;
            }
        }
        System.out.print("主持人筛选后剩下那扇门是车的次数为:"+j);
    }
}

多执行几次,查看结果:

主持人筛选后剩下那扇门是车的次数为:66822
主持人筛选后剩下那扇门是车的次数为:66719
主持人筛选后剩下那扇门是车的次数为:66741

概率大概为2/3,所以嘉宾更换选项获取车的几率更大!

后来对比别人做的发现,这差距有点大啊,我一点面向对象的思想都没有,真是惭愧啊,下面贴上大神代码:

package test;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ThreeDoors {

    private static final int doorNumber = 3;

    private static List doorList = new ArrayList(doorNumber);
    private static void init(){
        doorList.clear();
        for (int i = 0; i < doorNumber; i++) {
            doorList.add(new Door(i));
        }
    }

    private static int getRandomDoor(int doorNumber) {
        return new Random().nextInt(doorNumber);
    }

    private static void setCar() {
        int carDoorNumber = getRandomDoor(doorNumber);
        doorList.get(carDoorNumber).setBehind("Car");
        System.out.println("car:" + carDoorNumber);
    }

    private static void chooseDoor() {
        int chooseDoor = getRandomDoor(doorNumber);
        doorList.get(chooseDoor).setHasChoosed(true);
        System.out.println("choose:" + chooseDoor);
    }

    private static void openDoorWithoutCarBehind() {
        List doorsToOpen = new ArrayList();
        for (Door door : doorList) {
            if (door.isSheepBehind() && !door.hasChoosed) {
                doorsToOpen.add(door);
            }
        }
        doorsToOpen.get(getRandomDoor(doorsToOpen.size())).openDoor();
        System.out.println("open:" + doorsToOpen.get(getRandomDoor(doorsToOpen.size())).getDoorNumber());
    }

    private static void changeDoor() {
        for (Door door : doorList) {
            if (door.hasChoosed) {
                door.setHasChoosed(false);
                continue;
            }
            if (!door.hasChoosed && !door.hasOpened) {
                door.setHasChoosed(true);
                System.out.println("choose:" + door.getDoorNumber());
            }
        }
    }

    private static Boolean isWin() {
        for (Door door : doorList) {
            if (door.hasChoosed && door.isCarBehind()) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int beforeChangeWin = 0;
        int afterChangeWin = 0;
        for(int i = 0; i < 100000; i++) {
            init();
            setCar();
            chooseDoor();
            openDoorWithoutCarBehind();
            if(isWin()){
                beforeChangeWin ++;
                System.out.println("not Change Win");
                continue;
            }
            changeDoor();
            if(isWin()){
                afterChangeWin ++;
                System.out.println("change Win");
            }
        }
        System.out.println("===============");
        System.out.println("Not Change Win:" + beforeChangeWin);
        System.out.println("Change Win:" + afterChangeWin);
    }

    private static class Door {
        private String behind = "Sheep";
        private Boolean hasChoosed = false;
        private Boolean hasOpened = false;
        private int doorNumber;

        public Door(int doorNumber){
            this.doorNumber = doorNumber;
        }

        public boolean isSheepBehind() {
            return "Sheep".equals(behind);
        }

        public boolean isCarBehind() {
            return "Car".equals(behind);
        }

        public String getBehind() {
            return behind;
        }

        public void openDoor() {
            hasOpened = true;
        }

        public void setBehind(String behind) {
            this.behind = behind;
        }

        public Boolean getHasChoosed() {
            return hasChoosed;
        }

        public void setHasChoosed(Boolean hasChoosed) {
            this.hasChoosed = hasChoosed;
        }

        public int getDoorNumber() {
            return doorNumber;
        }

        public void setDoorNumber(int doorNumber) {
            this.doorNumber = doorNumber;
        }
    }
}

总结:以后思考问题,得多从面向对象去解决问题!

你可能感兴趣的:(android)