好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

1152: 零起点学算法59——找出一个数组中出现次数最多的那个元素

1152: 零起点学算法59——找出一个数组中出现次数最多的那个元素

Time Limit: 1 Sec   Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 990   Accepted: 532
[ Submit ][ Status ][ Web Board ]

Description

找出一个数组中出现次数最多的那个元素

 

Input

第一行输入一个整数n(不大于20)
第二行输入n个整数

多组数据

 

Output

找出n个整数中出现次数最多的那个整数(每行一个,保证唯一解)

 

Sample Input

 

 4
1 2 2 3 

 

Sample Output

 2
 

 

Source

零起点学算法

 

  1  #include<stdio.h>
  2   int   main(){
   3       int  n,a[ 20 ],b[ 20 ]={ 0  };
   4       while (scanf( "  %d  " ,&n)!= EOF){
   5           for ( int  i= 0 ;i<n;i++ ){
   6              scanf( "  %d  " ,& a[i]); 
   7           }
   8      
  9       for ( int  i= 0 ;i<n;i++ ){
  10           for ( int  j=i+ 1 ;j<n;j++ ){
  11               if (a[i]== a[j]){
  12                  b[i]++ ;
  13               }
  14           }
  15       }
  16      
 17       int  m=b[ 0  ];
  18       for ( int  i= 1 ;i<n;i++ ){
  19           if (b[i]> m)
  20          m= b[i];
  21       }
  22      
 23       for ( int  i= 0 ;i<n;i++ ){
  24           if (b[i]== m){
  25          printf( "  %d\n  "  ,a[i]);
  26           }
  27       }    
  28       }
  29       return   0  ;
  30  } 

 

查看更多关于1152: 零起点学算法59——找出一个数组中出现次数最多的那个元素的详细内容...

  阅读:51次