int i, n, stayWins = 0, switchWins = 0;
srand(time(NULL));
printf("请输入尝试次数:");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
prizeDoor = rand() % 3 + 1;
guess = rand() % 3 + 1;
do
{
open = rand() % 3 + 1;
} while(open == prizeDoor || open == guess);
remainingDoor = 6 - guess - open;
// 不换门
if(guess == prizeDoor)
stayWins++;
// 换门
if(remainingDoor == prizeDoor)
switchWins++;
}
printf("不换门胜率:%f\n", (double)stayWins / n);
printf("换门胜率:%f\n", (double)switchWins / n);
return 0;
上计算生物学提到的三门问题,第一反应有点反常规,仔细思考过后通过c语言用大量重复次数验证了正确答案(换门胜率2/3,不换门胜率1/3)。
标签:rand,guess,语言,胜率,prizeDoor,代码,三门,stayWins,open From: https://blog.51cto.com/u_16071620/6190402