(解题报告)HDU1004---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): 93885 Accepted Submission(s): 35848

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

解题思路:
1. 多个测试,用!=EOF与==1效果相同,输入0时不做操作;
2. 使用一个二维字符数组输入颜色,这样行下标控制个数,列下表控制输入的颜色;
3. 使用一个数组储存每输入一个颜色后相同的次数;
4. 每输入一个颜色后将其与之前的颜色做比较,使用strcmp(a,b)比较,相同则为0,使对应数组里的个数加1,如果之后再出现相同颜色做相同的计算,也只取最大的一项;
5. 使用动态比较找出数组中最大个数对应的行下标,输出对应的字符串;
6. 注意每次都要对字符串和数组清0;

代码如下:

#include <stdio.h>
#include <string.h>
int main ()
{
    char a[1000][20];
    int b[1000];
    int n,m,d=0,Max,k;
    while(scanf("%d",&m)!=EOF)
    {
        if(m)
        {
        memset(b,0,sizeof(b));
        memset(a,0,sizeof(a));
        for( k=0;k<m;k++)
        {
            scanf("%s",a[k]);
            for(int q=0;q<k;q++)
            {
                if(strcmp(a[k],a[q])==0)
                b[q]++;
               }
            }
        Max=-1;
        for(k=0;k<m;k++)
        {
            if(b[k]>Max)
            {
                Max=b[k];
                d=k;   
            }
        }
        printf("%s\n",a[d]);
        }
    }
    return 0;
}

仅表示个人观点!

你可能感兴趣的:((解题报告)HDU1004---Let the Balloon Rise)