1054 The Dominant Color

大致题意就是给出N行M列的元素,找出出现次数最多的元素并输出。

#include<iostream>
#include<unordered_map>
using namespace std;

int main() {
    unordered_map<int,int> mp;
    int m,n;
    cin>>m>>n; 
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            int t;
            scanf("%d",&t); //可能会超时,把cin改为scanf 
            mp[t]++;
        }
    }
    int max = -1,ans = 0;
    for(auto it = mp.begin(); it != mp.end(); ++it) {
        if(max < it->second) {
            max = it->second;
            ans = it->first;
        }

    }
    cout<<ans;
    return 0;
}

 

posted @ 2020-02-16 20:02  tangq123  阅读(119)  评论(0编辑  收藏  举报