STL_A1054 The Dominant Color (20 分)

https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768

/*
*map的使用
*访问map的键、值
*数字与出现次数的map映射
*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<map>

int main() {
    int m,n,color;
    scanf("%d%d",&m,&n);
    map<int,int> count; //数字与出现次数的map映射
    map<int,int>::iterator it;

    for(int i=0;i<m;i++) {
        for(int j=0;j<n;j++) {
            scanf("%d",&color);
            if(count.find(color) != count.end()) count[color]++;
            //若已经存在,则color出现次数加1,否则次数置为1
            else count[color]=1;
        }
    }
    int maxColor=0,maxCount=0;
    for(it=count.begin();it != count.end();it++) {
        if(it->second > maxCount) { //注意是maxCount
            maxColor=it->first;
            maxCount=it->second;
        }
    }
    printf("%d",maxColor);
    return 0;
}
posted @ 2019-08-17 11:38  2o2o  阅读(94)  评论(0编辑  收藏  举报