​【CCF-CSP】201312-1 出现次数最多的数​

题目

给 n 个数,求出现次数最多且值最小的数字。

分析

#include <bits/stdc++.h>
using namespace std;
#define d(x) cout<<x<<endl

typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10;

int n;
map<int, int> mp;

int main() {
scanf("%d", &n);
for (int i = 1, num; i <= n; i++) {
scanf("%d", &num);
mp[num]++;
}
int ans, maxn = 0;
for (auto it = mp.begin(); it != mp.end(); it++) {
if (it->second > maxn) {
maxn = it->second;
ans = it->first;
}
}
printf("%d\n", ans);
return 0;
}