c语言:随机产生的100个0到10之间的整数,设计程序,统计0到10的个数

急用,谢谢

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main( void )
{
/*count=100:随机产生100个整数, 对局部变量count数组任意一个赋值, 其他也都是0.*/
int i, count=100, acount[11]={0};
srand((unsigned)time(NULL)); //初始化随机数
while(count--)
{
/*根据需要多次调用rand(),从而不间断地得到新的随机数*/
i=rand()%11;
acount[i]++;
}
for(i=0; i<=10;i++)
printf("count of %d =%d\n", i, acount[i]);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-16
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main( void )
{
/*count=100:随机产生100个整数, 对局部变量count数组任意一个赋值, 其他也都是0.*/
int i, count=100, count[11]=0
srand((unsigned)time(NULL)); //初始化随机数

while(count--)
{
/*根据需要多次调用rand(),从而不间断地得到新的随机数*/
i=rand()%11;
switch(i){
case 0:
count[0]++;
break;
case 1:
count[1]++;
break;
case 2:
count[2]++;
break;
case 3:
count[3]++;
break;
case 4:
count[4]++;
break;
case 5:
count[5]++;
break;
case 6:
count[6]++;
break;
case 7:
count[7]++;
break;
case 8:
count[8]++;
break;
case 9:
count[9]++;
break;
case 10:
count[10]++;
break;
}
for(i=0; i<=10;i++)
printf("count of %d =%d\n", i, count[i]);
return 0;
}
相似回答