中国开发网: 论坛: 程序员情感CBD: 贴子 251070
周星驰
BS没脾气han上面还没明白的,并附上没脾气程序改为c后的代码han运行结果。。。
其实道理很简单,每个选择都有1/3的概率正确,第一次没选的两个门总共当然占有2/3,然后主持人还帮你排出一个不对的,但是客观分布情况不会再改变,怎么可能会再同第一次的选择平分选中的概率呢?

运行结果:
E:\yxd\studio\program\tests\test1\test1\Debug>test1

游戏者没有交换次数为 5025,其中得到车的次数为 1641
游戏者选择交换次数为 4975,其中得到车的次数为 3302
E:\yxd\studio\program\tests\test1\test1\Debug>test1

游戏者没有交换次数为 4934,其中得到车的次数为 1609
游戏者选择交换次数为 5066,其中得到车的次数为 3327
E:\yxd\studio\program\tests\test1\test1\Debug>test1

游戏者没有交换次数为 5088,其中得到车的次数为 1592
游戏者选择交换次数为 4912,其中得到车的次数为 3280

由没脾气代码改为c程序:
void Test()
{
int max = 10000;

bool m[3];

int r, s1, sx, s2;

int cNonexchangeAll = 0, cNonexchangeGot = 0;
int cExchangeAll = 0, cExchangeGot = 0;

srand( (unsigned)time( NULL ) );

for ( int i = 1; i <= max; i++ )
{
for(int j=0; j<3; j++)
{
m[j] = false;
}

r = rand() % 3;
m[r] = true;

s1 = rand() % 3;

if ( m[s1] )
{
sx = s1;
}
else
{
int kk[2];

int k=0;
for(int j=0; j<3; j++)
{
if ( j == s1 )
{
continue;
}

kk[k] = j;
k++;
}

sx = kk[rand() % 2];
}

bool isexchange = ( ( rand() % 2 ) == 0);

if ( isexchange )
{
s2 = 3 - sx - s1;// 0, 1, 2
//s2 = 6 - (sx + 1) - (s1 + 1) - 1;// = 3 - sx - s1;
cExchangeAll++;
}
else
{
s2 = s1;
cNonexchangeAll++;
}

if ( m[s2] )
{
if ( isexchange )
{
cExchangeGot++;
}
else
{
cNonexchangeGot++;
}
}
}
printf( "\r\n游戏者没有交换次数为 %d,其中得到车的次数为 %d ", cNonexchangeAll, cNonexchangeGot );
printf( "\r\n游戏者选择交换次数为 %d,其中得到车的次数为 %d ", cExchangeAll, cExchangeGot );
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录