一个IQ问题的C++模拟

来源:互联网 发布:嵌入式和单片机 编辑:程序博客网 时间:2024/04/28 20:41
无标题文档

一个 IQ 问题的 C++ 模拟

问题:

你在一个电视节目中玩游戏 . 这个游戏是赢一辆车 . 主持人展示你三个门 . 他告诉你三个门中有一个门后是车 , 另两个门后是羊 . 他让你从中选一个门 . 然后你从中选一个门 , 但是这个门暂时不打开 . 然后主持人打开剩下两扇门中的一扇后面是羊的 ( 因为主持人知道哪扇门后是羊哪扇门后是车 ). 然后他说你还有机会改主意 , 你想换一扇门呢还是想仍旧选择你原来选择的那扇门 ?

你应该怎么做 ?

解答 1 :换不换都一样, 1/2 机会中

不要跟过去的情况联系起来,只考虑目前情况,选择的时间是打开 1 个门后,而打开一个门后的情况是:

只有 2 个门,其中 1 个门有车,另一个门有羊,现在选择

其实之前你选了和没选都是一样的,情况就是 1 门有车 1 门有羊, 50% 中。

解答 2 :

肯定要换啊 !!!

夸张点 :

100W 个盒子中有一个有奖品 .

你选了一个

别人打开了 100W-2 个后发现都是空的 .

你会换吗 ?

不管怎样你是否有奖品是你第一次选择时就决定的 . 这个选中几率是很小的啊 .....

这样几乎可以肯定最后一个盒子里有奖 !!

看似 2 种解答都有道理,下面用 C++ 模拟一下游戏过程,看看换和不换到底是哪个更吃亏。

模拟的思想是用一个 N 个 int 数据的数组表示 N 个门,其中只有一个值为 1 的的表示车,其他全是 0 表示全部是羊。用户选择其中一个元素,然后告诉用户除了被选择的元素和另一个我们随机选择的一个元素外,其他全是羊,给用户机会再次选择,然后进行统计,看玩 X 遍之后得到多少车多少羊就知道哪个解答是对的了。

下面是程序。

源程序如下

#include<stdlib.h>
#include<iostream.h>
#include<time.h>
#include <string.h>
#define MAX 3//the total doors MAX<RAND_MAX

int doors[MAX];//MAX doors


void init()
{
srand(unsigned(time(0)));
for(int i=0;i<MAX;i++)
{
doors[i]=0;//0=sheep,set each door to sheep,
}
doors[int(rand())%MAX]=1;// 1=car,one of the door is car,whichi door is car is randomized
}

int main(int argc, char* argv[])
{
int times=0;
int cars=0;
int sheep=0;

int i=0;

int choose=0;// the first choose
int choose2=0;

int preserved[2];//the preserved 2 unopened doors

while(1)//loop playing
{
/////////////////////////////
cout<<"you play total:"<<times<<" ,and you got:"<<endl;
cout<<"cars: "<<cars<<endl;
cout<<"sheep: "<<sheep<<endl;
cout<<endl;

init();

cout<<"now there is "<<MAX<<" doors , 1 of the door is car , "<<MAX-1<<" of the doors are sheep,please choose one "<<endl;
cout<<"enter your choise(input a number):";
cin>>choose;

for(i=0;i<MAX;i++)
{
if(doors[i]==1)
{
preserved[0]=i;//keep the door with car unopened
}
}

preserved[1]=choose;//keep the other door which user chose
while(preserved[1]==preserved[0])//if the user chose the door that with car
{
srand(unsigned(time(0)));
preserved[1]=(int(rand())%MAX);// randomize another door unopen
}

cout<<endl;
cout<<"now i tell u, expect the door "<<preserved[1]<<"(which you chose) and door "<<preserved[0]<<"(unknown what inside) are unopened, all the other doors are opened, and there are all sheep,i give u a chance to change your mind"<<endl;
cout<<"please enter 0 for stick, or enter 1 for change:";
cin>>choose2;

char s[128];
if(choose2==0)
{
strcpy(s,"you stick");
}
else
{
strcpy(s,"you changed");
cout<<"ok , u change your choise to door"<<((choose==preserved[0])?preserved[1]:preserved[0])<<endl;
choose=(choose==preserved[0])?preserved[1]:preserved[0];
}

cout<<endl;
cout<<"ok "<<s<<",now i tell you the car is in door "<<preserved[0]<<endl;

if(choose==preserved[0])
{
cout<<"you get a car"<<endl;
cars++;
}
else
{
cout<<"you get a sheep"<<endl;
sheep++;
}
///////////////////////
times++;
}
return 0;
}

///////////////////////////////////////////////

最上面

#define MAX 3

定义门的个数,首先像题目一样是 3 个,这样比较难看出换与不换的区别,但当门的个数逐渐增加时(比如 100 个),如果玩 10 遍或者 20 遍或者更多都选择不换的话,几乎总是得到羊的。

由此可见解答 2 是对的,即主持人给你第二次选择的机会的话,一定要换!!

原创粉丝点击