HDU ACM1004——Let the Balloon Rise

Let the Balloon Rise

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

题目大意:给你一定数量的彩色气球,让你找出数量最多的是那种颜色的气球。

#include <stdio.h>
#include <string.h>
int main()
{
    int N=1,max,flag,i,j; 
    while(scanf("%d",&N)!=EOF&&N)
    {
        char color[1002][20]={0};
        int count[1002]={1};    // 注意第i个气球自身也要算上 
        for(i=0;i<N;i++)
            scanf("%s",color[i]);
        for(i=0;i<N-1;i++){ //依次寻找i之后与第i个气球相同的个数 
            for(j=i+1;j<N;j++){
                if(!strcmp(color[i],color[j]))
                    count[i]++;     //表示i之后有多少气球与其相同 
            }
        }
        max=count[0];flag=0;
        for (i=1;i<=N;i++){     //确定第几个气球相同个数最多 
            if (max<count[i]){
                flag=i;
                max=count[i];
            }     
        }
        printf("%s\n",color[flag]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/BarisGuo/article/details/82021418