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.
 

 

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
5 green red blue red red 3 pink orange pink 0
 

 

Sample Output
red pink

 解题思路:

题意就是让你找出出现次数最多的气球的颜色

一种方法是建立一个string数组,然后进行排序,然后再来寻找次数最多的那一个

另一种方法就是使用STL里面的map,直接寻找最大数字即可

 

代码如下:

 1 #include<bits/stdc++.h>
 2 #define mem(a) memset(a,0,sizeof(a))
 3 #define forn(i,n) for(int i=0;i<n;++i)
 4 #define for1(i,n) for(int i=1;i<=n;++i)
 5 #define IO std::ios::sync_with_stdio(false); std::cin.tie(0)
 6 using namespace std;
 7 typedef long long ll;
 8 const int maxn=1e5+5;
 9 const int mod=10007;
10 const int inf=0x3f3f3f3f;
11 
12 int n,m,t,k;
13 map<string,int> balloon;
14 
15 
16 int main()
17 {
18     IO;
19     while(cin>>n&&n)
20     {
21         balloon.clear();
22         string str,res;
23         int maxs=0;
24         for1(i,n)
25         {
26             cin>>str;
27             balloon[str]++;
28             if(balloon[str]>maxs)
29             {
30                 res=str;
31                 maxs=balloon[str];
32             }
33 
34         }
35         cout<<res<<endl;
36     }
37     return 0;
38 }

 

posted @ 2020-02-09 17:30  小松QAQ  阅读(166)  评论(0编辑  收藏  举报