gpt4 book ai didi

c - Monty Hall 游戏 : How to print which door the computer opened

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:00 25 4
gpt4 key购买 nike

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

int main(void)
{
static int games = 0;
static int stayWins = 0;
static int switchWins = 0;
int chosenDoor;
int remainingDoor;
int revealedDoor;
int winningDoor;
int option;

printf("Type 0 to stop choosing and print results: ");

srand (time(NULL));
do
{
printf("Choose door 1, 2, or 3: ");
scanf("%d",&chosenDoor);

if (chosenDoor==0)
break;

printf("Enter '1' for stay; Enter '2' for switch:");
scanf("%d",&option);

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)
{
printf("You win.\n");
stayWins++;
}
else
{
printf("You lose.\n");
}
}
if (option == 2)
{
chosenDoor = remainingDoor;
if (chosenDoor == winningDoor)
{
printf("You win.\n");
switchWins++;
}
else
{
printf("You lose.\n");
}
}
games++;
} while (chosenDoor!=0);

printf("Out of %d games, the contestant won %d times by staying with his/her original choice and won %d times by switching his/her choice.",games,stayWins,switchWins);

return 0;
}

这是运行 Monty Hall 游戏的代码,用户可以从三扇门中选择一扇门。一扇门有奖品,另外两扇是假的。用户选择门 1、2 或 3,并选择当程序打开其中一扇假门时是否切换门。

我怎样才能让程序打开一扇门,这扇门必须是一扇门,后面没有奖品,也没有被用户选择打印它的决定。

这是打印的内容:

...
Choose door (1,2,3):
Enter 1 for stay; 2 for switch:
You win/lose.
...

这是我要打印的内容:

...
Choose door (1,2,3):
Door X has been opened to reveal a false door.
Enter 1 for stay; 2 for switch:
You win/lose.
...

非常感谢您的帮助。谢谢你,干杯!

最佳答案

只需随机选择一扇门,如果门是所选门和/或奖品门,则递增或递减门数。

以 1,2,3,1,2,3,... 顺序递增到下一扇门:

door = door%3 + 1;

递减:

door = (door + 1)%3 + 1;

...或者只是递增两次。

关于c - Monty Hall 游戏 : How to print which door the computer opened,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com