杭电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): 118554    Accepted Submission(s): 46464


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
 

其实这个也没什么难的,就是使用map容器,每一次,输入,直接使用map[XXX]++就可以了

当然,每一次重新判断需要把map原来的元素清空,调用clear函数就行了

#include
#include
#include
#include
//#define LOCAL

using namespace std;

int main(int argc,char **argv)
{
    int n;
    #ifdef LOCAL
        freopen("data.in","r",stdin);
    #endif
    map My_map;
    string color;
    while(cin>>n)
    {
        if(n==0)
            break;
        My_map.clear();
        for(int i = 0; i < n; i++)
        {
            cin>>color;
            My_map[color]++;
        }
        int max = -1;
        for(auto it = My_map.begin(); it != My_map.end(); it++)
        {
            if(it->second>max)
                max = it->second;
        }
        for(auto it = My_map.begin(); it != My_map.end(); it++)
        {
            if(it->second==max)
            {
                cout<first<
    return 0;
}

下面 是我在讨论组里面看见的代码:



#include 
#include 
#include 
using namespace std;
int main()
{
	map a;
	int n, c = 0;
	string s;
	while (cin>>n&&n)
	{
		while (n--)
		{
			cin >> s;
			if (!a.count(s))a[s] = 0;
			a[s]++;

		}
		string ans;
		map::iterator it;
		int max = 0;
		for (it = a.begin(); it != a.end(); ++it)
		{

			if (a[it->first] > max)
			{
				max = a[it->first];
				ans = it->first;
			}
		}
		cout << ans << endl;
		a.clear();
	}
	return 0;
}

其实她的代码和我的差不多的,就是在找出出现次数最多的字符串的部分,我用了个笨方法,先遍历map找到最大的值,在遍历map去找到最大的值对应的索引值,相当于用了她一倍的时间

你可能感兴趣的:(C++,杭电oj)