[HDU1004] Let the balloon rise - 让气球升起来

[HDU1004] Let the balloon rise - 让气球升起来

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.

输入包含多组测试数据。每组数据以N(0 < N <= 1000)开始——放飞的气球总数。下面的N行各描述一个颜色。一个气球的颜色是一个最多有15个小写字母的字符串。

一组N为0的测试数据表示停止输入,并且这组数据不应被处理。

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

Sample Output

red

问题分析

这道题简而言之就是判断从终端输入次数最多的字符串。

解题思路

创建一个二维字符串数组a[][],用来存放颜色字符串,创建一个整形数组t[],当读到重复的颜色时我们可以得到这个颜色在a中对应的角标,假设为i,那我们将t[i]进行加一就可以起到计数的效果,直到所有颜色读完,找出t中最大的数,同样得到角标q,输出对应的颜色a[q]即可。

注意:

1.N的取值为0<N<=100。当N等于1时,输出输入的字符串;
当N等于2时,每个字符串都是出现次数最多的字符串,则选择任意一个字符串输出。
2.字符串用数组储存,数组长度选择尤为慎重,不然就会超时。

代码:

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

int main()
{
    int n;
    char a[1000][25];
    int t[1000];

    while (scanf("%d", &n), n != 0)
    {
        int i, j;
        for (i = 0; i < n; i++)
            scanf("%s", a[i]);
        for (i = 0; i < n; i++)
            t[i] = 0;
        int max = 0;
        int best;
        for (i = 0; i < n - 1; i++)
        {
            for (j = i + 1; j < n; j++)
                if (strcmp(a[i], a[j]) == 0)//用来判断两个数组是否为同一个字符串,是则累加。
                    t[i]++;
            if (t[i] > max)
            {
                max = t[i];
                best = i;
            }
        }
        printf("%s\n", a[best]);
    }

    return 0;
}
内心os:读到这里你可能跟我之前一样疑惑为什么要定义一个二维数组,难道吃饱了撑着(当然不是QAQ),我饭后一直在想,后来想通了一点觉得应该是便于对于输入的N个字符串,每个都进行一次判断是否为同一颜色,但我总觉得还有其他作用,至于是什么请各位看官留下你的见解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值