|
概率问题,下面的小程序足以说明问题
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#define SHEEP 1
#define CAR 2
#define SELECTTIMES 100000 // 选择十万次
int main()
{
int door[3] = {SHEEP, SHEEP, CAR};
time_t t;
srand((unsigned)time(&t));
int iCar = 0;
for (int iSelectTimes = 0; iSelectTimes < SELECTTIMES; iSelectTimes++)
{
int iSelect = rand() % 3; // 嘉宾随机选择
for (int j = 0; j < 3; j++)
{
if ((door[j] == SHEEP) && (j != iSelect)) // 主持人打开一扇有羊的门
{
break;
}
}
for (int k = 0; k < 3; k++)
{
if ((k != j) && (k != iSelect))
{
if (door[k] == CAR) // 另一扇门是汽车嘛?
{
iCar++;
}
}
}
}
printf("car if change %d\n", iCar);
return 1;
} |
|