HDU 1004 Let the Balloon Rise

来源:互联网 发布:录制游戏视频的软件 编辑:程序博客网 时间:2024/05/29 04:34
Problem Description
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. 
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

Sample Input
5greenredblueredred3pinkorangepink0
 

Sample Output
redpink

统计每个字符串出现的次数,可以创建两个数组,一个存字符串,一个存次数,最后再找到出现次数最多的,而map给我们提供了一套更完整的解决方案,可以自由的定义key和value的数据类型(当然必须得是可以用“<”号比较的)

#include<iostream>#include<string>#include<map>using namespace std;int main(){map<string,int>balloons;map<string,int>::iterator it;string max_b,ball;int n,max;while(scanf("%d",&n),n){balloons.clear();while(n--){cin>>ball;balloons[ball]++;}it=balloons.begin();max_b=it->first;max=it->second;for(it=balloons.begin();it!=balloons.end();it++)if(it->second>max){max=it->second;max_b=it->first;}cout<<max_b<<endl;}return 0;}












0 0
原创粉丝点击