首页 > 代码库 > zoj 2104
zoj 2104
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
int n,i,j;
string s;
map<string,int>m;
map<string,int> :: iterator it,it2;
while(cin>>n && n)
{
m.clear();
for(i=0;i<n;i++)
{
cin>>s;
if(m.find(s)!=m.end())//找到了就 加1
m[s]+=1;
else m[s]=1; //没找到就 =1,还可以再用,技巧,可省去vector的使用,边输入,边统计,迭代,高效。
}
it2=m.begin();
for(it=m.begin();it!=m.end();it++)
{
if(it2->second<it->second)
it2=it;//it2->second=it->second;
} //note that the exchanged thing is the whole not only parts of it
//in the iterator of it2 which is just a cup!
cout<<it2->first<<endl;
}
// system("pause");
return 0;
}
//新波动(754369601) 11:11:25
zoj 2104:
#include <iostream>
#include <map>
#include <string>
using namespace std;
map <string,int>my;
map <string,int>::iterator it;
int main(int argc, char *argv[])
{
int n;string s,s1;int Max;
while(cin>>n,n)
{
Max=0;
while(n--)
{cin>>s;my[s]++;}
for(it=my.begin();it!=my.end();it++)
{
if(Max<(*it).second)
{
Max=(*it).second;
s1=(*it).first;
}
}cout<<s1<<endl;
my.clear();
}
return 0;
}
********************************************************************************************************************8
//[--李吉环--]<lijihuan0@qq.com> 11:12:44
zoj 2104
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
map<string,int> m;
map<string,int>::iterator it;
int main()
{
int n;
char x[100];
while(scanf("%d",&n),n!=0)
{
m.clear();
while(n--)
{
scanf("%s",x);
m[x]++;
}
int max=0;
string color;
for(it=m.begin();it!=m.end();it++)
{
if(max<it->second)
{
color=it->first;
max=it->second;
}
}
printf("%s\n",color.c_str());
}
}