1054 The Dominant Color (20分)
1054
分析:
使用map记录每个数字出现的次数,然后遍历map找到出现次数最多的数字。
C++:
#include <cstdio>
#include <map>
using namespace std;
map<int, int> mp;
int main() {
int m, n, a;
scanf("%d%d", &m, &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a);
if (mp.count(a) == 0) {
mp[a] = 1;
} else {
mp[a] = mp[a] + 1;
}
}
}
int maxCount = -1, maxNum;
for (map<int, int>::iterator iter = mp.begin(); iter != mp.end(); iter++) {
if (iter->second > maxCount) {
maxCount = iter->second;
maxNum = iter->first;
}
}
printf("%d", maxNum);
return 0;
}










网友评论