-1

我目前正在编写 C++ Monty Hall 问题模拟,但遇到了一些麻烦。我不断收到的错误是:

source.cpp(23): error C4700: uninitialized local variable 'doorReveal' used
source.cpp(25): error C4700: uninitialized local variable 'doorSwitch' used
source.cpp(52): error C4700: uninitialized local variable 'stayWin' used
source.cpp(56): error C4700: uninitialized local variable 'switchWin' used

我似乎无法弄清楚出了什么问题。该项目应该通过首先在前 100 次尝试中保持原始门选择,然后在接下来的 100 次尝试中显示门时切换来模拟获胜。提前感谢大家的帮助。

 #include <iostream>
 #include <time.h>
 using namespace std;

 int main()
 {
     int doorChoice;
     int prizeDoor;
     int doorReveal;
     int doorSwitch;
     int count;
     int switchWin;
     int stayWin;

     srand((unsigned int) time(0));

     for (count = 0; count <= 200; count++)
     {
         prizeDoor = (rand() % 3) + 1;
         doorChoice = (rand() % 3) + 1;
         cout << "The prize door is door number " << prizeDoor << " ." << endl;
         cout << "The door the contestant chose was door " << doorChoice << endl;
         doorReveal != prizeDoor || doorChoice;
         cout << "The host revealed door number " << doorReveal << " ." << endl;
         doorSwitch != doorChoice || doorReveal;

         while (count < 101)
         {
             if (doorChoice == prizeDoor)
             {
                 cout << "Great Job! You won!" << endl;
             }
             else {
                 cout << "Not this time!" << endl;
             }
         }

         while (count < 201)
         {
             if (doorSwitch == prizeDoor)
             {
                 cout << "You switched and won!" << endl;
             }
             else {
                 cout << "You switched and lost!" << endl;
             }
         }

         if (doorChoice == prizeDoor)
         {
             stayWin++;
         }
         if (doorSwitch == prizeDoor)
         {
             switchWin++;
         }
         count++;
     }
     cout << "Your win percentage when staying was " << stayWin << "%!" << endl;
     cout << "Your win percentage when switching was " << switchWin << "%!" << endl;
     return 0;
 }
4

1 回答 1

10

问题1:operator!=

operator !=不做你认为它做的事。

你的意思

    doorReveal = !(prizeDoor || doorChoice);
    cout << "The host revealed door number " << doorReveal << " ." << endl;
    doorSwitch = (doorChoice || doorReveal);

在确定 doorReveal 时,我感觉到另一个逻辑问题。稍后我将不得不考虑这一点。编辑:见问题 5

问题2:while

while循环还有一个问题:

    while(count < 101)

    // ...

    while(count < 201)

它们是无限循环,因为在循环期间计数不会增加。我想你的意思是if在那里,而不是while.

问题 3:初始化switchWinstayWin

这些变量只会增加。就像@KonradRudolph 建议的那样,

  • 在第一次需要它们的地方声明你的变量
  • 初始化它们
  • 当你在它的时候,把它们标记const为适当的

问题4:rand()%3有偏见

您可能应该使用均匀分布。

问题 5:修复你的门“衍生”

布尔值不是集合。即使他们是,你也会被困在二元集。我提出以下模型:

enum doors { door1 = 1, door2 = 2, door3 = 4, any = door1|door2|door3 };

所以你可以说:

doors const doorReveal = doors(~(prizeDoor | doorChoice)  & any);
doors const doorSwitch = doors(~(doorChoice | doorReveal) & any);

修复该问题会导致程序似乎可以工作:

#include <iostream>
#include <time.h>
using namespace std;

enum doors { door1 = 1, door2 = 2, door3 = 4, any = door1|door2|door3 };

static inline std::ostream& operator<<(std::ostream& os, doors val) {
    switch(val) {
    case door1: return os << "door #1";
    case door2: return os << "door #2";
    case door3: return os << "door #3";
    case any:   return os << "any door";
    }
    return os << "OOPS";
}

int main()
{
    unsigned switchWin = 0;
    unsigned stayWin   = 0;
    srand((unsigned int) time(0));
    for(int count = 0; count <= 200; count++)
    {
        doors const prizeDoor  = doors(1 << rand() / ( RAND_MAX / 3 ));
        doors const doorChoice = doors(1 << rand() / ( RAND_MAX / 3 )); 
        cout << "The prize door is door number " << prizeDoor << " ." << endl;
        cout << "The door the contestant chose was door " << doorChoice << endl;
        doors const doorReveal = doors(~(prizeDoor | doorChoice)  & any);
        doors const doorSwitch = doors(~(doorChoice | doorReveal) & any);

        cout << "The host revealed door number " << doorReveal << " ." << endl;
        if(count < 101)
        {
            if(doorChoice == prizeDoor)
            {
                cout << "Great Job! You won!" << endl;
            }
            else
            {
                cout << "Not this time!" << endl;
            }
        };
        if(count < 201)
        {
            if(doorSwitch == prizeDoor)
            {
                cout << "You switched and won!" << endl;
            }
            else
            {
                cout << "You switched and lost!" << endl;
            }
        };
        if(doorChoice == prizeDoor)
        {
            stayWin++;
        }
        if(doorSwitch == prizeDoor)
        {
            switchWin++;
        };
        count++;
    }
    cout << "Your win percentage when staying was " << stayWin << "%!" << endl;
    cout << "Your win percentage when switching was " << switchWin << "%!" << endl;
    return 0;
}
于 2013-09-25T07:00:49.310 回答