HDU:Let the Balloon Rise

来源:互联网 发布:有人招聘网络写手吗? 编辑:程序博客网 时间:2024/04/30 03:37

Let the Balloon Rise

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


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

解题思路:1.每次输入一个字符串,就让它与数组中的字符串比较,如果发现相等的,就让数目加1,比较完毕后,让他与最大数目相比,如果大于最大数目,就更新
mmax的值,并更新字符串answer(答案)的值。
2.可以用映射去写,map(string,int)
本人代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n,mmax,i,j,ccount; //n是单词个数,mmax是最大个数,i,j为循环变量,ccount用来计数
    string color[1003],answer; //color为字符串数组,answer用来存放答案的临时变量
    while(cin>>n)
    {
        if(n==0) //n为0结束循环
            break;
        answer="0"; //answer赋初值"0"
        mmax=0; //最大值初始化为0
        for(i=0;i<n;i++) //依次输入单词
        {
            ccount=0; //每次输入一个新的单词,就将数量初始化为0
            cin>>color[i]; //输入单词
            for(j=0;j<=i;j++) //和当前字符串数组中的每一个字符串进行比较
                if(color[i]==color[j])
                    ccount++; //发现相同的,数量自增1
            if(ccount>mmax) //如果数量比最大值大
            {
                mmax=ccount; //最大值被更新
                answer=color[i]; //答案也被更新
            }
        }
        cout<<answer<<endl; //最后输出答案即可
    }
    return 0;
}


他人代码:
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    string temp,answer; //临时变量,temp,answer
    int n,mmax;
    while(cin>>n&&n)
    {
        mmax=0;
        map<string,int>m; //映射,
        while(n--)
        {
            cin>>temp; //输入一个单词
            m[temp]++; //该单词数目自增1
            if(m[temp]>mmax) //更新数据
            {
                mmax=m[temp];
                answer=temp;
            }
        }
        cout<<answer<<endl; //输出结果
    }
    return 0;
}

0 0
原创粉丝点击