HDU 1004 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): 7259    Accepted Submission(s): 2109
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.

思路:

每输入一种颜色,就拿去跟已有的颜色匹配,若匹配成功,则该颜色的num加1,否则,将该颜色加入颜色数组中去。

贴下代码:

#include<stdio.h>
#include<string.h>

struct
color
{

    int
num;
    char
StrColor[20];
}
color[1000];

int
Max,maxI;

void
clear()
{

    int
i;
    for
(i=0;i<1000;i++)
    {

        color[i].num=0;
        strcpy(color[i].StrColor,"");
    }
}



int
main()
{

    int
n;
    int
NumOfColor;
    char
TempColor[20];
    while
(EOF!=scanf("%d",&n)&&n)
    {

        int
i,j,flag;

        clear();

        scanf("%s",TempColor);
        strcpy(color[0].StrColor,TempColor);
        color[0].num=1;
        NumOfColor=1;

        for
(i=1;i<n;i++)
        {

            scanf("%s",TempColor);
            flag=1;
            for
(j=0;j<NumOfColor&&flag;j++)
            {

                if
(!strcmp(TempColor,color[j].StrColor))
                {

                    color[j].num++;
                    flag=0;
                }
            }

            if
(flag)
            {

                strcpy(color[NumOfColor++].StrColor,TempColor);
                color[NumOfColor-1].num=1;
            }
        }


       
        Max=color[0].num;
        maxI=0;
        for
(i=1;i<NumOfColor;i++)
        {

            if
(color[i].num>Max)
            {

                Max=color[i].num;
                maxI=i;
            }
        }

        printf("%s/n",color[maxI].StrColor);
    }

    return
0;
}

你可能感兴趣的:(String,ini,input,UP,each)