问题描述: 三门问题也就是“蒙提霍尔问题”,表述如下: 现在有三扇门,两扇门后是山羊,一扇门后是汽车. 参赛者并不知道内里有什么。 参赛者需要在三扇门中挑选一扇,期望是门后有汽车。 主持人知道每扇门后面有什么。 主持人必须开启剩下的其中一扇门,并且必须提供换门的机会。 主持人永远都会挑一扇有山羊的门。 如果参赛者挑了一扇有山羊的门,主持人必须挑另一扇有山羊的门。 如果参赛者挑了一扇有汽车的门,主持人随机在另外两扇门中挑一扇有山羊的门。 参赛者会被问是否保持他的原来选择,还是转而选择剩下的那一道门。 转换选择可以增加参赛者的机会吗?

#include <stdlib.h>
#include <time.h>
#include <stdio.h>

int test()
{
    int box = 3;
    int right ; //正确的
    int us; //用户选择
    int ch; // 改主意
    int drop; //丢弃的id
    int status ;
    right = rand() % 3;
    us = rand() % 3;
    ch = rand() % 2;
    drop = rand() % 2;
    if(right == us)
        drop = us > drop ? drop : drop+1;
    else
        drop = 3 - us - right;
    //printf("%d %d %d %d\n",right,drop,us,ch);
    ch  = 1; //强制改变
    if(ch) us = 3 - us - drop;
    status = (us == right);
    //printf("%d %d %d %d [%d]\n",right,drop,us,ch,status);
    return us == right;
}

int main()
{
    long all = 1000000000;
    long win = 0;
    double per ;
    srand((unsigned) time(NULL));
    for(long i=0;i<all;i++)
    {
        win += test();
        per = (double)win/(double)(i+1);
        //
        //printf("%f -- w:%ld a:%ld r",per,win,i+1);
        //getchar();
    }
    per = (double)win/(double)all ;
    printf("\n%f -- w:%ld a:%ld \n",per,win,all);
    return 0;
}

执行结果:

$ gcc sim.c -o sim && ./sim

0.666623 -- w:666622815 a:1000000000

当然换

自己试试

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *