使用了STL中的map和string,使得代码的体积小了很多,下面是代码。
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int count;
cin>>count;
while(count != 0)
{
map< string, int > si;
while(count)
{
string temp;
cin>>temp;
si[temp]++;
--count;
}
int maxnumber = 0;
string s;
for(map< string, int >::iterator it = si.begin(); it != si.end(); it++)
{
if(it->second > maxnumber)
{
maxnumber = it->second;
s = it->first;
}
}
cout<<s<<endl;
cin>>count;
}
return 0;
}
#include <map>
#include <string>
using namespace std;
int main()
{
int count;
cin>>count;
while(count != 0)
{
map< string, int > si;
while(count)
{
string temp;
cin>>temp;
si[temp]++;
--count;
}
int maxnumber = 0;
string s;
for(map< string, int >::iterator it = si.begin(); it != si.end(); it++)
{
if(it->second > maxnumber)
{
maxnumber = it->second;
s = it->first;
}
}
cout<<s<<endl;
cin>>count;
}
return 0;
}