这个门数可以自己更改
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#define LEN 999999
#define How_Many_Doors 100
bool Test(bool IsChange)
{
vector<bool> Doors(How_Many_Doors, false);
int realCarLocation = rand() % How_Many_Doors;
Doors.at(realCarLocation) = true;
int ChallengerFirst = rand() % How_Many_Doors;
bool ChangeYourMind = IsChange;
if (ChallengerFirst != realCarLocation)
{
return ChangeYourMind ? true : false;
}
else
{
return ChangeYourMind ? false : true;
}
}
int main()
{
srand(time(0));
int WinTimes1 = 0, WinTimes2 = 0;
for (unsigned long i = 0; i < LEN; i++)
{
if (Test(true))
WinTimes1++;
if (Test(false))
WinTimes2++;
}
cout << fixed << setprecision(4) << "Change: " << 100.00 * WinTimes1 / LEN << "%" << endl;
cout << fixed << setprecision(4) << "Not Change: " << 100.00 * WinTimes2 / LEN << "%" << endl;
return 0;
}
-
若门数为 100 100 100
-
若门数为 3 3 3
多试几组数字,可以发现若门数为n,那么换门赢的概率为 n − 1 n \frac{n-1}{n} nn−1,不换门赢的概率为 1 n \frac{1}{n} n1