HDU 1004 Let the Balloon Rise 一道Map的经典题目
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.
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.
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
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
题目的意思就是你有很多气球,每种气球有不同的颜色,求最多气球的颜色。
题目不难,关键是通过这道题我会用了map。
代码如下:
1 #include<cstdio> 2 #include<map> 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 string s1; 7 int main(){ 8 int n; 9 string maxcolor; 10 map<string,int>mapint; 11 int x=0; 12 while (cin>>n&&n ) 13 { 14 mapint.clear(); 15 for (int i=1;i<=n;++i) 16 { 17 cin>>s1; 18 mapint[s1]++; 19 } 20 map<string, int>::iterator i; 21 int maxx=0; 22 for( i=mapint.begin();i!=mapint.end();++i) 23 { 24 if(i->second>maxx) 25 { 26 maxx=i->second; 27 maxcolor=i->first; 28 } 29 } 30 cout<<maxcolor<<endl; 31 } 32 return 0; 33 }