c program

2007-12-15 9:16 pm
write a program to simulate rolling a dice for 600 times.determine the number of outcomes which are divisble by 2 or 3.check that the result shounld be around 4000.

回答 (1)

2007-12-16 2:47 pm
✔ 最佳答案
/* here is the program requested. As you probably understand, it will produce
numbers around 400 as the result, and not 4000, as we throw only 600 times.
However, you'll get 4000 if you change the value of nTimes to 6000.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
const int nTimes=600;
srand(time(NULL));
int i,k, sum=0;
for(i=0;i<nTimes;i++)
{
k=rand()%6+1;
if(k%2==0||k%3==0)sum++;
}
printf("Number of throws=%d\nNumber of outcomes divisible by 2 or 3=%d\n",nTimes,sum);
return 0;
}


收錄日期: 2021-04-13 14:43:34
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20071215000051KK01541

檢視 Wayback Machine 備份