1. 程式人生 > >HDU1004 Let the Balloon Rise 解題報告

HDU1004 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.

又到了比賽時間了!看到氣球在周圍升起是多麼的激動人心啊!(ACM中,選手每解決一道問題,就可以在桌子上升起一個對應顏色的氣球)但是告訴你一個祕密,裁判最喜歡的時刻是猜測最流行的題目。當比賽結束,他們會數出每種顏色氣球的個數。

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.

輸入包括多個測試用例,每個測試用例開始於一個數N(0 < N <= 1000),是氣球的總個數。接下來N行包括每一個的顏色。氣球的顏色是一個不超過15個小寫字母的字串。

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 <iostream>
#include <string.h>

using namespace std;

const int MAX_BALLOONS_NUM =1000;

int N;

char temp[15+1];
char balloons[MAX_BALLOONS_NUM][15+1];
long int  balloons_num[MAX_BALLOONS_NUM];
int now_num = 0;

int main()
{
    while(1)
    {
        scanf("%d",&N);
        
        if( N == 0 )
        {
            return 0;
        }
        
        if( N == 1 )
        {
            scanf("%s",temp);
            printf("%s\n",temp);

            continue;
        }
        
        memset(balloons,0,sizeof(balloons));
        
        for( int k = 0; k < N; k++ )
        {
            balloons_num[k] = 0;
        }
        
        for( int k = 0; k < N; k++ )
        {
            scanf("%s",temp);
            
            int i = 0;
            
            for( i = 0; i < now_num; i++ )
            {
                if( 0==strcmp(temp,balloons[i]))
                {
                    balloons_num[i]++;
                    break;
                }
            }
            
            if( i == now_num )
            {
                strcpy( balloons[i],temp );
                balloons_num[i]++;
                now_num++;
            }
        }
        
        int max = balloons_num[0], pos = 0;
            
        for( int k = 0; k < now_num; k++ )
        {
            if( balloons_num[k] > max )
            {
                max = balloons_num[k];
                pos = k;
            }
        }
    
        printf("%s\n",balloons[pos]);
    }
    
    return 0;
}