*数组-10. 求整数序列中出现次数最多的数

 1 /*
 2  * Main.c
 3  * E10-数组-10. 求整数序列中出现次数最多的数
 4  *  Created on: 2014年8月22日
 5  ***********部分通过*********
 6  */
 7 
 8 #include <stdio.h>
 9 
10 void bubbleSort(int array[], int lenth) {
11     int i, j;
12     for (i = 0; i < lenth; i++) {
13         for (j = 0; j < lenth - i - 1; j++) {
14             if (array[j] > array[j + 1]) {
15                 int temp = 0;
16                 temp = array[j];
17                 array[j] = array[j + 1];
18                 array[j + 1] = temp;
19             }
20         }
21     }
22 }
23 
24 int main(void) {
25 
26     int N, array[1001];
27     int maxValue = 0, maxCount = 0; //出现次数最多的数和最多次数
28     int i,j;
29 
30     scanf("%d", &N);
31     for (i = 0; i < N; i++)
32         scanf("%d", &array[i]);
33     //按照升序将数组排序
34     bubbleSort(array, N);
35     //如果前一个数与后一个数相等,则计次,否则不计次
36     int count = 0;
37     for (i=0;i < N;i++) {
38         for(j=i+1;j<N&&(array[i] == array[j]);j++)
39                 count++;
40             if (count > maxCount) {
41                 maxCount = count;
42                 maxValue = array[i];
43             }
44             count=0;
45     }
46 
47     printf("%d %d\n", maxValue, maxCount+1);
48 
49     return 0;
50 }

 

题目链接:

http://pat.zju.edu.cn/contests/basic-programming/%E6%95%B0%E7%BB%84-10

参考:

http://blog.csdn.net/rabbit8848/article/details/30753847

 

posted @ 2014-08-23 00:32  Boomkeeper  Views(943)  Comments(0Edit  收藏  举报
最好的流量统计 网站流量计数器