[size=medium]Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
code as following
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int nCount;
while(cin>>nCount && nCount)
{
string color;//存放输入的颜色的临时变量
map<string,int> mapBalloon;//用map容器存放气球颜色及出现次数
while(nCount--)
{
cin>>color;//输入颜色
mapBalloon[color]++;
//因为是使用数组方式存入map容器,当输入的颜色已存在于容器关键字中时,不会重复存入容器,
//容器中的值是自动初始化为0的,所以这里只把此颜色关键字对应的值+1
}
map<string, int>::iterator iter;//遍历
int max=0;
for(iter = mapBalloon.begin(); iter != mapBalloon.end(); iter++)
{//第一次遍历标记最大次数所在位置
if(iter->second > max)
max = iter->second;
}
for(iter = mapBalloon.begin(); iter != mapBalloon.end(); iter++)
{//第二次遍历找到标记的位置
if(iter->second == max)
cout<<iter->first<<endl;
}
}
return 0;
} [/size]
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
code as following
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int nCount;
while(cin>>nCount && nCount)
{
string color;//存放输入的颜色的临时变量
map<string,int> mapBalloon;//用map容器存放气球颜色及出现次数
while(nCount--)
{
cin>>color;//输入颜色
mapBalloon[color]++;
//因为是使用数组方式存入map容器,当输入的颜色已存在于容器关键字中时,不会重复存入容器,
//容器中的值是自动初始化为0的,所以这里只把此颜色关键字对应的值+1
}
map<string, int>::iterator iter;//遍历
int max=0;
for(iter = mapBalloon.begin(); iter != mapBalloon.end(); iter++)
{//第一次遍历标记最大次数所在位置
if(iter->second > max)
max = iter->second;
}
for(iter = mapBalloon.begin(); iter != mapBalloon.end(); iter++)
{//第二次遍历找到标记的位置
if(iter->second == max)
cout<<iter->first<<endl;
}
}
return 0;
} [/size]