医疗体检车:用C++编程 请帮我写出完整的程序 谢谢

来源:百度文库 编辑:高考问答 时间:2024/05/03 07:02:24
口袋里有相同的3只红球,4只白球。随机取出3只球来,然后放回袋中,如此共取1000次。问取到3只球都是红球或白球的概率是多少?

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int balls[]={0,0,0,1,1,1,1};//0:red, 1:white

void main()
{
int count=0;
int a, b, c;
srand((unsigned int)time(NULL));

for(int i=0;i<1000;i++)
{
a=balls[(int)(7*rand()/RAND_MAX)];
b=balls[(int)(7*rand()/RAND_MAX)];
c=balls[(int)(7*rand()/RAND_MAX)];
if(a==b && b==c)
count++;
}

printf("\n%f",(float)count/1000);
}

//VC 环境下编译