【HUD】1004 : Let the Balloon Rise

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weclove2008/article/details/79120623

Let the Balloon Rise

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

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
 

Sample Output
 
    
redpink
 

Author
WU, Jiazhi
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1008  1005  1009  1019  1021 
 
 

思路:

 
1.由于scanf("%d",&n)的返回值为成功输入的数目,cin的返回值为输入变量的内存地址。所以不能作为检测是n否为0的标准,此处的方法是在循环中判0并跳出,也可以使用scanf(cin>>n&&n)作乘操作判断结果;

2.具体方法是创建一个字符串数组,C++的库为<string>,string类型支持直接判等操作,组件数组后一边输入一边进行检查。

3.每次输入之后检查与之前输入的字符串是否相等并进行比较计数,然后得出出现次数最多的字符串,将其保存在pop变量中,如果n==1那么让pop赋值为仅有的一个元素。

4.由于题目的特殊性:所以不需要减去已经检查过的字符串(结果相同),也不需要对出现次数相等字符串操作,因此节约了很多代码。


代码:

#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

int main()
{
    string str[1001]; //up to 1000 kinds of colour
    int n,max,count;
    string pop;
    while(cin>>n)
    {
        if(n==0)break;
        max=0;
        for(int i=0; i<n; i++)
        {

            count=0;
            cin>>str[i]; //input one color string
            if(n==1)pop=str[i];
            for(int j=0; j<i; j++)
            {

                if(str[i]==str[j])
                    {
                        count++;
                    }
            }

            if(count>max)
            {
                max=count;
                pop=str[i];
            }

        }

        cout<<pop<<endl;
    }





    return 0;
}





猜你喜欢

转载自blog.csdn.net/weclove2008/article/details/79120623