PAT:1054. The Dominant Color (20) AC(map法)

#include<stdio.h>
#include<map>
using namespace std;
const int MAX=0x3fffffff;
int main()
{
  int m,n;
  map<int,int> count;        //数字与出现次数的map映射
  scanf("%d%d",&m,&n);
  for(int i=0 ; i<n ; ++i)
  {
    for(int j=0 ; j<m ; ++j)
    {
      int tmp;
      scanf("%d",&tmp);
      if(count.find(tmp)!=count.end())  //存在,次数+1
        ++count[tmp];
      else                //不存在,次数设为1
        count[tmp]=1;
    }
  }
  int ans=-1,MAXtmp=-1;
  for(map<int,int>::iterator it=count.begin() ; it!=count.end() ; ++it)
  {
    if(it->second>MAXtmp)          //【skill】map映射的第一第二位置的使用
    {
      ans=it->first;
      MAXtmp=it->second;
    }
  }
  printf("%d
",ans);
  return 0;
}
原文地址:https://www.cnblogs.com/Evence/p/4328692.html