题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1004
这道题如果用stl的map容器会非常简单。因为我对map容器尚不是很了解,故将此题记录下来。
在一开始的代码中,我设定每输入一次颜色就进行一次迭代比较,如果发现这种颜色,则 it->second+=1。在我的代码AC之后,我去百度了一下,发现其实并不用这么麻烦,一行代码就可以搞定的:color[str]++。
不多说了,以下是G++下AC的代码,大家有什么建议可以和我一起讨论:
#include<iostream>
#include<map>
using namespace std;
main(){
int N,i,max,flag;
string str;
map<string,int>color;
map<string,int>::iterator it;
while(cin>>N&&N){
for(i=N;i>0;i--){
cin>>str;
color[str]++;
}
max=0;
for(it=color.begin();it!=color.end();it++){
if(it->second>max){
max=it->second;
str=it->first;
}
}
cout<<str<<endl;
color.clear();
}
}