sdnu oj 1125 Let the Balloon Rise


1125.Let the Balloon Rise

Time Limit: 1000 MS    Memory Limit: 32768 KB
Total Submission(s): 295    Accepted Submission(s): 120

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

Source

© 2012-2015 SDNU ACM-ICPC TEAM, v1.2 Beta

看到这个题,最后需要把一个字符串和数值绑在一起嘛,第一反应是用结构体,后来做着做着突然想到了map,一个键值对应一个value值,刚刚好,所以就用map做了。而且用map的话,它的效率据查阅资料是lgN,也不会T掉。

我的代码:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
    int ballnum;
    while(cin>>ballnum && ballnum!=0)
    {
        string temp;
        int bigger=0;
        map<string,int> balloon;
        for(int i=0;i<ballnum;i++)
        {
            cin>>temp;
            balloon[temp]++;
        }
        m
ap<string,int>::iterator it;//这个就表示从weight开始迭代,相当于循环里的i变量,for(int i =0



for(it=balloon.begin();it!=balloon.end();it++) {

            if((*it).second>bigger)  bigger=(*it).second;
        }
        for(it=balloon.begin();it!=balloon.end();it++)
        {
            if((*it).second==bigger) cout<<(*it).first<<endl;
        }
    }
    return 0;

}

做题时我对stl也不是很熟悉很熟悉,就搜到了下面的一些知识,方便以后使用呗:

iterator 迭代器类型
iterator  it 
it->frist表示对象的第一个类型,一般是key.
it->second表示对象的第二个类型,一般是val

这里的意思是  it->first,是weith第一个类型,int
it->second是weigtht第二个类型  double 
比如 weigth有2个元素。(10, 12.1)  ( 20, 20.3)
那么第一次迭代时,
it->first = 10  it->second= 12.1
继续
it->first = 20, it->second = 20.3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值