zoukankan      html  css  js  c++  java
  • 三门问题

    刚刚在A站看到这个三门问题,通过"主持人知道三扇门后面是什么"这条线索想出了答案.结论是:换的话胜率大一些.
    为什么呢?
    假设我们一定会换门,那么当我们选择门a的时候,有三种情况(图):


    因为这出现三种情况的概率都是1/3,所以假设我们一定会换门,那么我们有2/3的概率赢(情况2或3),只有1/3的概率输(情况1).所以换门的概率大一些.

    下面我们写个程序来证明这个结论:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define TEST_TIMES 10000
     5 int main(void)
     6 {
     7         long unsigned times = TEST_TIMES;
     8         long unsigned win_times = 0;
     9         srand(time(NULL));
    10         while (times--)
    11                 if ((rand()%3) != 0) /* 1/3概率选到车子 */
    12                         win_times ++;
    13         printf("%lf
    ", (double)win_times / (double)TEST_TIMES);
    14 
    15         return 0;
    16 }


    结果:
    louis@debian:~$ gcc three_doors.c -o tf
    louis@debian:~$ while :; do ./tf; sleep 1; done
    0.667400
    0.661300
    0.662700
    0.664700
    0.663200
    0.665100
    0.659000
    0.666900
    0.672900
    0.672100
    0.664100
    0.671400
    0.666400
    0.662800
    0.677500
    0.676200
    0.652100
    0.672800
    0.661500
    0.663800
    0.660500
    0.670300
    0.668400
    0.655900
    0.670300
    0.666600
    0.668400
    0.659500
    0.664400
    0.663900
    0.672400
    0.672100
    0.666000
    0.664000
    0.667600
    0.668500
    0.659400
    0.658100
    0.657400
    0.665800
    0.665400
    ^C
    louis@debian:~$

    可以看出,换门的话,胜率接近与2/3且比不换的胜率大,这与我们的结论是相符的.

  • 相关阅读:
    事务与锁的一些总结
    NYOJ 73
    NYOJ 456
    Sleep函数
    NYOJ 488(素数环)
    NYOJ 308
    NYOJ 27
    NYOJ 325
    NYOJ 138
    求两个或N个数的最大公约数(gcd)和最小公倍数(lcm)的较优算法
  • 原文地址:https://www.cnblogs.com/KM-Y/p/three_doors_question.html
Copyright © 2011-2022 走看看