美文网首页
PAT甲级A1054---map的常见用法

PAT甲级A1054---map的常见用法

作者: 1nvad3r | 来源:发表于2020-07-23 10:41 被阅读0次

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;
}

相关文章

网友评论

      本文标题:PAT甲级A1054---map的常见用法

      本文链接:https://www.haomeiwen.com/subject/qmsclktx.html