HDU - 1004 E - Let the Balloon Rise【STL之map】

Description

在ACM比赛中,你每解决一道题,你就可以获得一个气球,不同颜色的气球代表你解决了不同的问题。在WJL同学参加的一场ACM比赛中,他发现场面上有N个气球,并熟练的说出了气球的颜色。

请你编写一个程序,找出气球数量最多的颜色。

Input

有多组样例输入。

每组样例第一行输入一个整数N (0 < N <= 1000) ,代表一共有N个气球。若N=0,则代表输入结束,你不需要输出任何信息。
接下来N行每行输入一个不多于15个字母的字符串代表颜色。

Output

对于每组样例数据,在单独的一行内输出数量最多的那种颜色的气球。(数据保证输出是唯一的) 

Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

code:

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(){
    map<string, int>mp;
    string color, MaxColor;
  	int n;
    while(cin>>n,n){
        mp.clear();//清0 
        while(n--){
            cin>>color;
            mp[color]++;
        }
        map<string, int>::iterator it;
    	int max = 0;
        for(it=mp.begin(); it!=mp.end(); it++){
            if(it->second>max){
                max = it->second;
                MaxColor = it->first;
            }
        }
        cout<<MaxColor<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41333844/article/details/81710907