玛丽莲问题模拟:“玛丽莲问题”中最著名的是“Behind Monty Hall’s Doors“,简称“The
Monty Hall Problem”。问题如下:
台上有三个门,一个后边有汽车,其余后边是山羊。主持人让你任意选择其一。
然后他打开其余两个门中的一个,你看到是山羊。这时,他给你机会让你可以重选,
也就是你可以换选另一个剩下的门。那么,你换不换?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define GOAT 0
#define CAR 1
#define N 10000
typedef int PRIZE;
int shuffle(const void* a, const void* b) { return rand() % 3 - 1; }
int main() {
srand(time(NULL));
int noChangedSuccess = 0, i, changedSuccess = 0;
int userSelected = 0;
PRIZE prizes[3] = {GOAT, GOAT, CAR};
for (i = 0; i < N; i++) {
qsort(prizes, 3, sizeof(PRIZE), shuffle);
if (prizes[userSelected] == CAR) ++noChangedSuccess;
}
printf("noChangedSuccess=%d ", noChangedSuccess);
for (i = 0; i < N; i++) {
qsort(prizes, 3, sizeof(PRIZE), shuffle);
if (prizes[1] == GOAT)
userSelected = 2;
else
userSelected = 1;
if (prizes[userSelected] == CAR) ++changedSuccess;
}
printf("changedSuccess=%d ", changedSuccess);
return 0;
}