1125.Let the Balloon Rise
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
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]++;
}
map<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;
}
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