• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Just Fools.
♕Heart-broken is just a fool.
   首页    新随笔    联系   管理    订阅  订阅

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

 

题目的意思就是你有很多气球,每种气球有不同的颜色,求最多气球的颜色。

题目不难,关键是通过这道题我会用了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 }

 

posted @ 2016-02-02 18:10  ♕Heart-broken  阅读(131)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3