写完发现和算法笔记几乎一模一样。。。(注意使用iter时不要写成 for(auto iter:colors) 了)但是看到柳神给了一个更好的方式:
m[temp]++; 当m中没有temp的时候自动赋值为0,所以一句话相当于省略了判断的过程QAQ
咱的:
#include<map>
#include<cstdio>
using namespace std;
int main(){
int m,n,maxn=-1,color=-1,temp;
scanf("%d %d",&m,&n);
map<int,int>colors;
map<int,int>::iterator iter;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&temp);
if (colors.find(temp)==colors.end())
colors[temp]=1;
else
colors[temp]++;
}
}
for(iter=colors.begin();iter!=colors.end();iter++){
if (iter->second>maxn){
maxn=iter->second;
color=iter->first;
}
}
printf("%d",color);
return 0;
}
改了后的:
#include<map>
#include<cstdio>
using namespace std;
int main(){
int m,n,maxn=-1,color=-1,temp;
scanf("%d %d",&m,&n);
map<int,int>colors;
map<int,int>::iterator iter;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&temp);
colors[temp]++;
}
}
for(iter=colors.begin();iter!=colors.end();iter++){
if (iter->second>maxn){
maxn=iter->second;
color=iter->first;
}
}
printf("%d",color);
return 0;
}