HDU 1004 Let the Balloon Rise

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 73297    Accepted Submission(s): 27379


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
 

Author
WU, Jiazhi
 

Source
 



学习map容器的使用。map容器的基础题,没什么好说的。代码如下:

#include <iostream>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
int n,maxn;
string maxc;
//char* a;
string a;
map<string, int> bollon;
int main()
{
    while(scanf("%d",&n)&&n!=0)
    {
        int i;
        bollon.clear();//多次输入,使用前注意清空bollon
        for(i=0;i<n;i++)
        {
            //scanf("%s",a);
            cin>>a;
            bollon[a]++;
        }
        maxn=0;
        map<string ,int>::iterator ite;
        for(ite=bollon.begin();ite!=bollon.end();ite++)
            if(ite->second>maxn)
            {
                maxn=ite->second;
                maxc=ite->first;
            }
        cout<<maxc<<endl;
    }
    return 0;
}





附录:关于map容器的一些知识:

C++中map容器提供一个键值对容器,map与multimap差别仅仅在于multiple允许一个键对应多个值。   

一、map的说明  
  1   头文件 
  #include   <map> 
  
  2   定义 
  map<string,   int>   my_Map; 
  或者是typedef     map<string,   int>   MY_MAP; 
  MY_MAP   my_Map; 
  
  3   插入数据 
  (1)   my_Map["a"]   =   1; 
  (2)   my_Map.insert(map<string,   int>::value_type("b",2)); 
  (3)   my_Map.insert(pair<string,int>("c",3)); 
  (4)   my_Map.insert(make_pair<string,int>("d",4)); 
  
  4   查找数据和修改数据 
  (1)   int   i   =   my_Map["a"]; 
            my_Map["a"]   =   i; 
  (2)   MY_MAP::iterator   my_Itr; 
            my_Itr.find("b"); 
            int   j   =   my_Itr->second; 
            my_Itr->second   =   j; 
  不过注意,键本身是不能被修改的,除非删除。 
  
  5   删除数据 
  (1)   my_Map.erase(my_Itr); 
  (2)   my_Map.erase("c"); 
  还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。 
  
  6   迭代数据 
  for   (my_Itr=my_Map.begin();   my_Itr!=my_Map.end();   ++my_Itr)   {} 
  
  7   其它方法 
  my_Map.size()               返回元素数目 
  my_Map.empty()       判断是否为空 
  my_Map.clear()           清空所有元素 
  可以直接进行赋值和比较:=,   >,   >=,   <,   <=,   !=   等等 
  
  更高级的应用查帮助去吧,^_^;


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值