HDOJ 1004 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>
char colors[1000][20];
int number[1000];
bool equalnumber(char a[],char b[])
{
    int k=0;
    while(a[k]!='\0'&&b[k]!='\0'&&a[k]==b[k])
        k++;
    if(a[k]=='\0'&&b[k]=='\0')return true;
    else return false;
}
int main()
{
    int n;
    while(scanf("%d",&n)&&n!=0)
    {
        int length=0;
        while(n--)
        {
            bool flag=false;
            char a[20];
            scanf("%s",a);
            for(int i=0;i<length;i++)
            {
                if(equalnumber(colors[i],a))
                {
                    number[i]++;
                    flag=true;
                    break;
                }
            }
            //printf("%d\n",flag);
            if(flag==false)
            {
                int k=0;
                while(a[k]!='\0')
                {
                       colors[length][k]=a[k];
                       k++;
                }
                colors[length][k]='\0';
                number[length]=1;
                length++;
                //printf("%s\n",colors[length]);
            }
        }
        //for(int i=0;i<length;i++)
        //{
        //    printf("%s %d\n",colors[i],number[i]);
        //}
        int maxnumber=0,maxindex;
        for(int i=0;i<length;i++)
        {
            if(number[i]>maxnumber)
            {
                maxnumber=number[i];
                maxindex=i;
            }
        }
        printf("%s\n",colors[maxindex]);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,797评论 0 23
  • 每当看着睡着的小可爱,我就会忍不住想要亲吻她,发自内心的说这孩子真的太让人疼爱了。二零一六年的九月小可爱成为了一名...
    我是问夏阅读 422评论 7 2
  • 假如 我和您没有任何关系 只以一个陌生人的身份 我会觉得你的儿女多么可恶 假如 这个只是一场噩梦 无论多吓人 我都...
    落红几许阅读 187评论 0 1
  • 1、启动指定的应用程序,并向其发送100个伪随机事件 * 示例:adb shell monkey -p packa...
    小喜_ww阅读 412评论 0 5