蒙特霍问题
写一个模拟程序来解决蒙特霍问题。程序应该模拟这个问题10000次,每次都随机选择奖品位置,并统计在保持初始情况下赢得汽车的次数,以及在换门后赢得汽车的次数。程序必须准确的模拟选择门,开门,然后改变门的过程。
(用以下程序不知道怎么的,两种策略下的次数统计要么都是0要么一个是0一个是10000,求高手指点)
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int doorA,doorB,doorC,choice;
int change_count=0, //保持初始选择情况下赢得汽车次数
nochange_count=0;//换门情况下赢得汽车次数
for(int i=0;i<100000;i++)
{
srand(time(NULL));
doorA=rand()%3+1;
doorB=rand()%3+1;
doorC=rand()%3+1;
choice=rand()%3+1;
if(doorA==doorB&&doorB!=doorC)//车在C门后边
{
if(choice==doorA)
change_count++;
else if(choice==doorC)
nochange_count++;
}
if(doorB==doorC&&doorB!=doorA)//车在A门后边
{
if(choice==doorB)
change_count++;
else if(choice==doorA)
nochange_count++;
}
if(doorA=doorC&&doorA!=doorB)//车在B门后边
{
if(choice==doorA)
change_count++;
else if(choice==doorB)
nochange_count++;
}
}
cout<<"不改变情况获得车的次数:"<<nochange_count<<endl;
cout<<"在改变情况获得车的次数:"<<change_count<<endl;
return 0;
}
[解决办法]
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
enum {A,B,C};
int carDoor;
int choice;
int change_count=0, //保持初始选择情况下赢得汽车次数
nochange_count=0;//换门情况下赢得汽车次数
srand(time(NULL));
for(int i=0;i<100000;i++)
{
// place car
carDoor=rand()%3;
//doorA=rand()%3+1;
//doorB=rand()%3+1;
//doorC=rand()%3+1;
// make a choice
choice=rand()%3;
// anchor open an empty door, this is actually irrelevant
// but just to make it looks more real.
int doorOpened= A!=choice&&A!=carDoor?A:B!=choice&&B!=carDoor?B:C;
// suppose there are two participants, one always change after anchor
// opened an empty door, while the other insists with original choice
int newchoice= A!=choice&&A!=doorOpened?A:B!=choice&&B!=doorOpened?B:C;
// now we open all door and check who wins
if(choice==carDoor)
nochange_count++;
if(newchoice==carDoor)
change_count++;
//
//if(doorA==doorB&&doorB!=doorC)//车在C门后边
//{
//if(choice==doorA)
//change_count++;
//else if(choice==doorC)
//nochange_count++;
//}
//if(doorB==doorC&&doorB!=doorA) //车在A门后边
//{
//if(choice==doorB)
//change_count++;
//else if(choice==doorA)
//nochange_count++;
//}
//if(doorA=doorC&&doorA!=doorB) //车在B门后边
//{
//if(choice==doorA)
//change_count++;
//else if(choice==doorB)
//nochange_count++;
//}
}
cout<<"不改变情况获得车的次数:"<<nochange_count<<endl;
cout<<"在改变情况获得车的次数:"<<change_count<<endl;
return 0;
}
#include <iostream>
#include <ctime>
int main()
{
using namespace std;
int carDoor, choice,
count=0, //total test performed
win=0; //换门情况下赢得汽车次数
srand(time(NULL));
for(int i=0;i<100000;i++)
{
// place car
carDoor=rand()%3;
// make a choice
choice=rand()%3;
if(carDoor==choice)
++win;
++count;
}
cout<<"不改变情况获得车的次数:"<<win<<endl;
cout<<"在改变情况获得车的次数:"<<count-win<<endl;
return 0;
}