题目描述
算法设计+题目分析
分析题目:
1、建立一个数到次数的映射map<int, int> mp;
2、每读取一个数,对应映射的次数+1;
3、遍历这个映射,找到其中次数最多的数,打印这个数。
注意点+记录点
无
代码(100分)
环境
Visual C++6.0
#include <stdio.h>
#include <map>
using namespace std;
int main() {
int n;
scanf("%d", &n);
map<int, int> mp;//建立数到次数到映射
int temp=0;
for (int i=0; i<n; i++){
scanf("%d", &temp);
mp[temp]++;//相应的数的次数+1
}
int re_i=0;//次数最多的数
int re_n=0;//次数最大值
for (map<int, int>::iterator it=mp.begin(); it!=mp.end(); it++){
if (it->second > re_n){//找到新的次数最多的数
re_i=it->first;
re_n=it->second;
}
}
printf("%d", re_i);
return 0;
}
感谢链接
无