【问题标题】:Monty Hall Game: How to allow user to choose door instead of program automatically randomizing choice?Monty Hall Game:如何让用户选择门而不是程序自动随机选择?
【发布时间】:2012-10-28 21:54:19
【问题描述】:
#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 次试验?

【问题讨论】:

    标签: c random printf scanf


    【解决方案1】:

    有没有办法可以将第一选择应用于其他 9,999 次试验?

    移动

    printf("Choice: ");
    scanf("%d", &chosenDoor);
    

    循环外的部分代码。

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 2020-02-19
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多