题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1004
思路:
用map<string,int> ,键为对应的color,值对应的次数
代码:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <map>
using namespace std;
map<string, int> m;
int main()
{
int n;
int i, j, k;
string color;
while (cin >> n)
{
m.clear();
map<string, int>::iterator it;
if (n == 0)
break;
for (i = 0; i < n; i++)
{
cin >> color;
if ((it = m.find(color)) == m.end())
{
m.insert(pair<string,int>(color,1));
}
else
{
m[color] ++;
}
}
string max;
int maxnum = 0;
for (it = m.begin(); it != m.end(); it++)
{
if (it->second > maxnum)
{
maxnum = it->second;
max = it->first;
}
}
cout << max << endl;
}
return 0;
}