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

题目的大概意思是给你N个颜色不同的气球让你从中找到出现次数最多的那种颜色,输出出现次数最多的那种颜色。

解题思路:

第一种:就是简单的字符串处理,先存到一个二维的字符串数组中,然后在拿出来一一遍历找出现次数最多的那个就可以了。

第二种:用map容器,用map将气球颜色存到mapfirst中,将出现的次数存到map的second中,然后用迭代器遍历找到最多次数的那个就可以了。

第一种方法代码如下:

#include
#include
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {   getchar();
        char s[1010][20];
        for(int i=0; imaxx)
                {maxx=sum;
                 k=i;
                }
        }
       printf("%s\n",s[k]);
    }
    return 0;
}

第二种map容器的方法如下:

#include
#include 
#include 
#include 
using namespace std;
int main()
{

    int n;
    while (scanf("%d",&n) && n)
    {
        map m;
        string s;
        while (n--)
        {
            cin >> s;
            m[s]++;
        }
        int max0 = 0;
        string maxx;
        map::iterator it;
        for (it = m.begin(); it != m.end(); it++)
        {
            if ((*it).second > max0)
            {
                max0 = (*it).second;
                maxx = (*it).first;
            }
        }
        cout << maxx << endl;

    }

    return 0;

}

 

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