HDU--1004 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 Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

题解:求某种颜色 最多的气球,输出这个颜色。

三个数组,一个输入字符串,一个存次数,一个记录此次数对应的字符串,

(发现我自己写的真是无敌绕了,偏偏还就记住了。。。。等之后再用容器做一遍吧。。。)

贴代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int a[1010],b[1010],f[1010];
    while(~scanf("%d",&n)&&n)
    {
        int i,j,t=0;
        char mp[1010][20];
        memset(a,0,sizeof(a));
        memset(f,0,sizeof(f));
        memset(b,0,sizeof(b));
        for(i=0; i<n; i++)
            scanf("%s",mp[i]);
        for(i=0; i<n; i++)
            for(j=i+1; j<n; j++)
            {
                if(f[j]==1)
                    continue;
                if(strcmp(mp[i],mp[j])==0)
                {
                    f[j]=1;
                    a[i]++;
                    b[a[i]]=i;
                }
            }
        sort(a,a+n);
        printf("%s\n",mp[b[a[n-1]]]);
    }
}

猜你喜欢

转载自blog.csdn.net/gjlfly/article/details/81108696