Skip to content
Go back

HDOJ1004 Let the Ballon Rise

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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.

Input

Input contains multiple test cases. Each test case starts with a numberN (0 < N <= 1000)— the total number of balloons distributed. The nextNlines contain one color each. The color of a balloon is a string of up to15lower-case letters.

A test case withN = 0terminates 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

Code

方法一

使用map容器

#include <bits/stdc++.h>
using namespace std;
// #define usefre

int main()
{
#ifdef usefre
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n;
    while(cin>>n&&n)
    {
        map<string,int> elements;
        while(n--)
        {
            string tmp;
            cin>>tmp;
            if(elements.find(tmp)!=elements.end())
            {
                elements[tmp]++;
            }
            else
            {
                elements[tmp]=1;
            }
        }
        map<string,int>::iterator it,ittmp;
        int max=-1000;
        for(it=elements.begin();it!=elements.end();it++)
        {
            if((*it).second>max)
            {
                max=(*it).second;
                ittmp=it;
            }
        }
        cout<<(*ittmp).first<<endl;
    }
    return 0;
}

方法二

使用结构体

#include <bits/stdc++.h>
using namespace std;
// #define usefre

typedef struct
{
    string color;
    int count;
} ballon;

int main()
{
#ifdef usefre
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n;
    while (cin >> n && n)
    {
        ballon b[1005];
        int num = 0;
        while (n--)
        {
            string tmp;
            cin >> tmp;
            int isNewColor = 1;
            for (int i = 0; i < num; i++)
            {
                if (tmp == b[i].color)
                {
                    b[i].count++;
                    isNewColor = 0;
                    break;
                }
            }
            if (isNewColor)
            {
                b[num].color = tmp;
                b[num].count = 1;
                num++;
            }
        }
        int p_max, max = 0;
        for (int i = 0; i < num; i++)
        {
            if (b[i].count > max)
            {
                max = b[i].count;
                p_max = i;
            }
        }
        cout << b[p_max].color << endl;
    }
    return 0;
}

Share this post on:

Previous Post
牛客网暑期ACM多校训练营2
Next Post
Ubuntu18.04美化