#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;
}
晚上的人, 这是一个完整的Monty Hall问题代码,用于打印10,000个游戏的结果。代码将为用户选择所选的门。如何更改它以便程序允许用户自己选择? 同样,我如何修改它以便程序不会随机化程序的“1”或“2”值,而是允许我选择切换? 我的进步...... 而不是:
chosenDoor = rand() % 3 + 1;
使用此选项,只有可接受的输入为1,2或3:
printf("Choice:");
scanf("%d",&chosenDoor);
这是正确的轨道吗?我知道此时用户需要在程序“完成”之前输入他的选择10,000次,那么有没有办法将第一选择应用于其他9,999次试验?
答案 0 :(得分:1)
我有没有办法将第一选择应用于其他9,999次试验?
移动
printf("Choice: ");
scanf("%d", &chosenDoor);
循环外的部分代码。