问题描述
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
AC CODE
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string, int> mp;
map<string,int>::iterator it;
int main()
{
int T;
string tmp;
string color;
int num;
while (cin >> T && T!=EOF && T!=0) {
mp.clear();
num = 0;
color = "";
for (int i = 0; i < T; i++) {
cin >> tmp;
mp[tmp]++;
}
for(it=mp.begin();it!=mp.end();++it){
if (num < it->second) {
color = it->first;
num = it->second;
}
}
cout << color << endl;
}
return 0;
}
解析
- map遍历方法