//上机练习1
//******************************************************
//统计软件下载次数最多的前十个,和下载次数为0的软件个数
//******************************************************
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 15
void swap(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
void main()
{
int num[N];
int count[N];
srand((unsigned)time(NULL));
//初始化数组
for(int i=0;i<N;i++)
{
num[i]=i+1;
count[i]=rand()%N;
printf("%2d:%3d\n",num[i],count[i]);
}
//冒泡排序
for(int j=0;j<N-1;j++)
{
for(int k=0;k<N-1-j;k++)
{
if(count[k]<count[k+1])
{
swap(&count[k],&count[k+1]);
swap(&num[k],&num[k+1]);
}
}
}
//结果打印输出
printf("前五名为:");
for(int k=0;k<5;k++)
{
printf("%3d",num[k]);
}
printf("\n");
}
1: 8
2: 0
3: 13
4: 11
5: 7
6: 9
7: 1
8: 8
9: 4
10: 9
11: 6
12: 5
13: 6
14: 2
15: 8
前五名为: 3 4 6 10 1
请按任意键继续. . .