遍一个小程序中的问题!

[复制链接]
查看11 | 回复2 | 2007-1-28 17:45:23 | 显示全部楼层 |阅读模式
随机产生0-9个数 是什么意思??0个数是什么样子?
回复

使用道具 举报

千问 | 2007-1-28 17:45:23 | 显示全部楼层
C代码int arr[10] = {0};
//统计次数的数组,初始化为0int i;int num;int first, second, third;int count;for(i = 0; icount){
count = arr;
first = i;
}}arr[first] = -1;
//讲出现次数的数的次数改成-1,下面再做,统计出来的就是第二多的数了second = 0;count = 0;for(i = 0; icount){
count = arr;
second = i;
}}arr[second] = -1;third = 0;count = 0;for(i = 0; icount){
count = arr;
third = i;
}}
回复

使用道具 举报

千问 | 2007-1-28 17:45:23 | 显示全部楼层
我的理解是随机输入40个0到9之间的数,统计出现次数最多的三个数.不知道是不是这个意思.程序如下,后面附上结果:#include #include void main(){ time_t t; time(&t); srand(t); int r,count[10]={0}; for(int i=0;i<40;i++){r = rand() % 10;count[r]++;printf("%d\t",r); } printf("\n\n"); int max=0,pos=0; for(i=0;i<10;i++){if(max < count){ pos = i; max = count;} } printf("出现次数最多的数:%d,出现了%d次\n",pos,max); count[pos] = 0; max = pos = 0; for(i=0;i<10;i++){if(max < count){ pos = i; max = count;} } printf("出现次数第二多的数:%d,出现了%d次\n",pos,max); count[pos] = 0; max = pos = 0; for(i=0;i<10;i++){if(max < count){ pos = i; max = count;} } printf("出现次数第三多的数:%d,出现了%d次\n",pos,max);}执行结果:8
0
4
9
5
4
2
3
5
79
8
6
9
7
5
9
9
1
20
9
8
9
1
3
6
4
1
90
6
9
0
6
6
3
4
6
5出现次数最多的数:9,出现了9次出现次数第二多的数:6,出现了6次出现次数第三多的数:0,出现了4次Press any key to continue
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行