求一个数组中出现次数最多的数
来源:互联网 发布:mac 迅雷播放器 编辑:程序博客网 时间:2024/05/07 23:32
描叙:一大推数据里面,数字与数字之间用空格隔开,找出出现次数最多的一个数字的算法
#include<stdio.h>void FindMostTimesDigit(int *Src , int SrcLen){int element , has = SrcLen;int MaxNum , TempCount = 0 , MaxCount = 0;int i , j;int *result = new int[];while(has != 0){element = Src[has - 1];TempCount = 0;for(i = has - 1 ; i >= 0 ; --i){//如果找到,则计数加1 ,然后将数据和末尾交换//这也是为何要从末尾开始循环的理由if(element == Src[i]){TempCount++;Src[i] = Src[has - 1];has--;}}if(TempCount > MaxCount){MaxCount = TempCount;MaxNum = 0;result[MaxNum] = element;}else if(TempCount == MaxCount){result[++MaxNum] = element;}}printf("出现次数最多的次数:%d\n" , MaxCount);for(j = 0 ; j <= MaxNum ; ++j)printf("%d " , result[j]);printf("\n");}int main(){int list[] = {1,2,3,4,3,3,2,2,1,1,4,4,4,1,2};int length = sizeof(list) / sizeof(int);FindMostTimesDigit(list , length);return 0;}
C++解法如下:
#include<iostream>#include<map>#include<utility>using namespace std;int main(){map<int , int> word_count;int number;while(cin>>number){pair<map<int , int>::iterator , bool> ret = word_count.insert(make_pair(number , 1));if(!ret.second)++ret.first->second;}for(map<int , int>::iterator iter = word_count.begin() ; iter != word_count.end() ; ++iter)cout<<(*iter).first<<"\t\t" <<(*iter).second<<endl;return 0;}
更简洁的方法如下:
#include<iostream>#include<map>#include<utility>using namespace std;int main(){map<int , int> word_count;int number;while(cin>>number)++word_count[number];for(map<int , int>::iterator iter = word_count.begin() ; iter != word_count.end() ; ++iter)cout<<(*iter).first<<"\t\t" <<(*iter).second<<endl;return 0;}
- 求一个数组中出现次数最多的数
- 求一个数组中出现次数最多的数
- 数组中出现次数最多的数及出现次数
- 数组-10. 求整数序列中出现次数最多的数(15)
- 数组-10. 求整数序列中出现次数最多的数
- 数组-10. 求整数序列中出现次数最多的数(15)
- 数组-10. 求整数序列中出现次数最多的数(15)
- 数组-10. 求整数序列中出现次数最多的数(15)
- 数组-10. 求整数序列中出现次数最多的数(15)
- 数组-10. 求整数序列中出现次数最多的数(15)
- 给定一个正整数组(最多1024个数,数组输入以0结束 ),求出现次数最多的数
- 求整数序列中出现次数最多的数
- hdu 1806 Frequent values(给定一个非降序数组,求任意区间内出现次数最多的数的次数)
- 求出现次数最多的数
- 求出现次数最多的数
- 找出数组中出现次数最多的数
- 找出数组中出现次数最多的数
- 求一个字符串中连续出现次数最多的字串
- 创建sql 用户登录
- 安卓界面布局那些事
- 数据库基础介绍
- 黑马程序员——Java基础---网络编程
- (step7.2.2)hdu 2161(Primes——判断是否是素数)
- 求一个数组中出现次数最多的数
- oracle数据库恢复与备份
- 上班一个月,我的几点体会
- 【微信实战案例:微信卖酒 月销5万】
- GBK和UTF-8编码的一些探索。
- 每月自评之七:2013年7月
- 图像滤波
- 成功人士必备的17个非凡特质
- LPSTR、LPCSTR、LPTSTR、LPCTSTR、LPWSTR及LPCWSTR的意义及区别