求一个数组中出现次数最多的数

http://blog.csdn.net/hyg0811/article/details/10797731

描叙:一大推数据里面,数字与数字之间用空格隔开,找出出现次数最多的一个数字的算法


  1. #include<stdio.h>   
  2.   
  3. void FindMostTimesDigit(int *Src , int SrcLen)  
  4. {  
  5.     int element , has = SrcLen;  
  6.     int MaxNum , TempCount = 0 , MaxCount = 0;  
  7.     int i , j;  
  8.     int *result = new int[];  
  9.     while(has != 0)  
  10.     {  
  11.         element = Src[has - 1];  
  12.         TempCount = 0;  
  13.         for(i = has - 1 ; i >= 0 ; --i)  
  14.         {  
  15.             //如果找到,则计数加1 ,然后将数据和末尾交换  
  16.             //这也是为何要从末尾开始循环的理由   
  17.             if(element == Src[i])  
  18.             {  
  19.                 TempCount++;  
  20.                 Src[i] = Src[has - 1];  
  21.                 has--;  
  22.             }  
  23.         }  
  24.   
  25.         if(TempCount > MaxCount)  
  26.         {  
  27.             MaxCount = TempCount;  
  28.             MaxNum = 0;  
  29.             result[MaxNum] = element;  
  30.         }  
  31.         else if(TempCount == MaxCount)  
  32.         {  
  33.             result[++MaxNum] = element;  
  34.         }  
  35.     }  
  36.   
  37.     printf("出现次数最多的次数:%d\n" , MaxCount);  
  38.     for(j = 0 ; j <= MaxNum ; ++j)  
  39.         printf("%d " , result[j]);  
  40.     printf("\n");  
  41.   
  42. }  
  43.   
  44. int main()  
  45. {  
  46.     int list[] = {1,2,3,4,3,3,2,2,1,1,4,4,4,1,2};  
  47.     int length = sizeof(list) / sizeof(int);  
  48.   
  49.     FindMostTimesDigit(list , length);  
  50.   
  51.     return 0;  
  52. }  


C++解法如下:

  1. #include<iostream>   
  2. #include<map>   
  3. #include<utility>   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     map<int , int> word_count;  
  9.     int number;  
  10.     while(cin>>number)  
  11.     {  
  12.         pair<map<int , int>::iterator , bool> ret = word_count.insert(make_pair(number , 1));  
  13.         if(!ret.second)  
  14.             ++ret.first->second;  
  15.     }  
  16.   
  17.     for(map<int , int>::iterator iter = word_count.begin() ; iter != word_count.end() ; ++iter)  
  18.         cout<<(*iter).first<<"\t\t"  
  19.             <<(*iter).second<<endl;  
  20.   
  21.     return 0;  
  22. }  



 

更简洁的方法如下:

  1. #include<iostream>   
  2. #include<map>   
  3. #include<utility>   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     map<int , int> word_count;  
  9.     int number;  
  10.   
  11.     while(cin>>number)  
  12.         ++word_count[number];  
  13.   
  14.     for(map<int , int>::iterator iter = word_count.begin() ; iter != word_count.end() ; ++iter)  
  15.         cout<<(*iter).first<<"\t\t"  
  16.             <<(*iter).second<<endl;  
  17.   
  18.     return 0;  
  19. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值