Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 120236 Accepted Submission(s): 47141
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.
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
这是我做的第一道map类题, 这一题题意很简单,就是让你输出出现次数最多的那种颜色。
一下只用map来求解这道题:
一下只用map来求解这道题:
方法一:
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
using namespace std;
map<string, int> ans;
int main()
{
int n;
string s;
while(~scanf("%d", &n))
{
ans.clear();
if(n ==0)
break;
else
{
for(int i = 0; i < n; i++)
{
cin >> s;
ans[s]++;
}
map<string, int> ::iterator it;
int ma = -100;
string mas;
for(it = ans.begin(); it != ans.end(); it++)
{
if((*it).second > ma)
{
ma = (*it).second;
mas = (*it).first;
}
}
cout << mas << endl;
}
}
return 0;
}
方法二:
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
using namespace std;
map<string, int> ans;
int main()
{
int n;
string s;
while(~scanf("%d", &n))
{
ans.clear();
if(n == 0)
break;
else
{
for(int i = 0; i < n; i++)
{
cin >> s;
ans[s]++;
}
map<string, int>::iterator p, m;
p = m = ans.begin();
for(; p != ans.end(); p++)
{
if(p->second > m->second)
{
m = p;
}
}
cout << m->first << endl;
}
}
return 0;
}