Easy - Let the Balloon Rise
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
| Inputcopy | Outputcopy |
|---|---|
5 green red blue red red 3 pink orange pink 0 |
red pink |
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
using namespace std;
int n;
string c[1000];
ios_base::sync_with_stdio(false);
while (cin>>n, n!=0)
{
int i;
for (i=0; i<n;i++) cin >>c[i];
if (n==1||n==2)
{
cout <<c[0] << endl;
continue;
}
sort(c,c+n);
int ct=1;
int mx=0;
string&best=c[0];
for (i=1; i<n;i++)
{
if (c[i]==c[i-1]) ct++;
else ct=1;
if (ct>mx)
{
mx=ct;
best=c[i];
}
}
cout <<best<<endl;
}
return 0;
}
博客围绕气球颜色统计问题展开,输入包含多组测试用例,每组先给出气球总数N,接着N行是每种气球颜色,以N = 0结束输入。要求输出每组中最流行问题对应的气球颜色,且保证有唯一解。
4947





