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
ZJCPC2004
思路:这道题,一开始我想了很多,比如用map把string和int存起来,然后用vector把map里的内容存起来再排序。后来事实证明是我想多了,完全可以用map的迭代器把map里的东西存入结构体,再排个序,输出就完事儿~
#include <cstdio>
#include <iostream>
#include <cmath>
#include <
string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
map<
string,
int>
mp;
struct node
{
string s;
int num;
} r[1000+
8];
int n;
string si;
bool cmp(node a, node b)
{
return a.num>
b.num;
}
int main()
{
while(~scanf(
"%d", &n) && n !=
0)
{
mp.clear();
for(
int i =
0; i<n; i++
)
{
cin>>
si;
if(mp[si])mp[si]++
;
else mp[si] =
1; }
int id =
0;
for(map<
string,
int>::iterator ii = mp.begin(); ii != mp.end(); ii++
)
{
r[id].s = ii->
first;
r[id].num = ii->
second;
id++
;
}
sort(r, r+
id, cmp);
cout<<r[
0].s<<
endl;
}
return 0;
}
转载于:https://www.cnblogs.com/RootVount/p/10981994.html
相关资源:JAVA上百实例源码以及开源项目