- UID
- 153
- 帖子
- 2398
- 积分
- 34637
- 阅读权限
- 80
- 注册时间
- 2005-7-9
- 最后登录
- 2015-6-19
- 在线时间
- 6842 小时
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main(void)
{
int i,n,bouns,select1,select2,nobouns,sum1=0,sum2=0;
time_t t;
srand((unsigned)(time(&t)));
printf("how many times do you want to try?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
bouns=rand()%3;//选定有奖金的门
select1=rand()%3;//选定第一次挑的门
nobouns=select1;
while(nobouns==bouns||nobouns==select1)
nobouns=rand()%3;//主持人开的门
select2=select1;
while(select2==select1||select2==nobouns)
select2=rand()%3;//换了的门
if(select1==bouns) sum1++;
else sum2++;
}
printf("If change: %d, %.2f percents,\nelse: %d, %.2f percents,\ntotal: %d\n",sum2,sum2*100.0/n,sum1,sum1*100.0/n,n);
return 0;
} |
|