HDU OJ 1004 Let the Balloon Rise

HDU OJ 1004 Let the Balloon Rise

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 60483 Accepted Submission(s): 22247


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 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

题意:给你很多字符串,让你找到数量最多的字符串,而且保证数量最多的字符串是唯一的
本题其实直接利用string读入然后直接利用string可比较大小排序后遍历每一个内容并记录每个对应项的个数然后按顺序从大到小把对应项输出就行了。
注意:题目中所指的气球不仅仅是气球,其实是对应的字符串。你把各种颜色列出来是没用的。

HDU1004.cpp
#include <iostream>
#include <string>
using namespace std;
string a[1000];
string end[1000];
int e[1000];
int main()
{
    string temp;
    int n;
    while(cin>>n,n)
    {
        for(int i = 0;i < n; i ++)
        {
            cin>>a[i];
        }
        for(int i = 0;i < n;i ++)
        {
            int flag = i;
            string t = a[i];
            for(int j = i+1;j < n;j++)
            {
                if (a[j]>a[flag])
                {
                    flag = j;
                    t = a[j];
                }
            }
            temp = a[i];
            a[i] = a[flag];
            a[flag] = temp;
        }
        /*
        for(int i = 0;i < n; i ++)
        {
            cout<<a[i]<<" ";
        }
        
*/
        int f2 = 0;
        end[0] = a[0];
        for(int i = 0;i < n; i ++)
        {
            if (end[f2]==a[i])
            {
                e[f2] += 1;
            }
            else
            {
                f2 +=1;
                end[f2] = a[i];
                e[f2] +=1;
            }
        }
        int max = 0;
        for(int i = 0;i < n; i ++)
        {
            if (e[i]>e[max])
            {
                max = i;
            }
        }
        cout<<end[max]<<endl;
    }
    //system("pause");
    return 0;
}

你可能感兴趣的:(HDU OJ 1004 Let the Balloon Rise)