#include <stdio.h>
#include <time.h>
int main(void)
{
int games = 0;
int stayWins = 0;
int switchWins = 0;
int chosenDoor;
int remainingDoor;
int revealedDoor;
int winningDoor;
int option;
srand (time(NULL));
do
{
chosenDoor = rand() % 3 + 1;
winningDoor = rand() % 3 + 1;
do
{
revealedDoor = rand() % 3 + 1;
} while (revealedDoor == chosenDoor || revealedDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == chosenDoor || remainingDoor == revealedDoor);
option = rand() % 2 + 1;
if (option == 1)
{
if (chosenDoor == winningDoor)
{
stayWins++;
}
}
if (option == 2)
{
chosenDoor = remainingDoor;
if (chosenDoor == winningDoor)
{
switchWins++;
}
}
games++;
} while (games < 10000);
printf("Out of 10,000 games, the contestant won %d times by staying with his/her original choice and won %d times by switching his/her choice.",stayWins,switchWins);
return 0;
}
晚安,这是一个完整的蒙蒂霍尔问题代码,它打印了 10,000 场比赛的结果。代码将为用户选择所选的门。我该如何更改它,以便程序允许用户自己选择?同样,我该如何修改它,以便程序不会为程序随机化“1”或“2”的值,而是允许我选择切换?我的进步......而不是这个:
chosenDoor = rand() % 3 + 1;
使用这个,只有可接受的输入是 1、2 或 3 :
printf("Choice:");
scanf("%d",&chosenDoor);
这是正确的轨道吗?我知道此时用户需要在程序“完成”之前输入他的选择 10,000 次,那么有没有办法可以将第一选择应用于其他 9,999 次试验?