hdu-1004 Let the Balloon Rise

该问题来自HDU-1004,要求找出一组输入字符串中出现次数最多的颜色。给定每个测试用例的气球总数N(0 < N <= 1000),接着是N行每行一个最多15个字母的 lowercase 字符串表示颜色。确保每个测试用例都有一个唯一的解决方案。输入以N=0结束,不作处理。解题思路是使用一个二维数组记录字符串及其出现次数,最后输出出现次数最多的颜色。
摘要由CSDN通过智能技术生成

题目来源:hdu-1004

Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 91599 Accepted Submission(s): 34868

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

Author
WU, Jiazhi

Source
ZJCPC2004
题目大意:
输入一个n,表示有n个字符串的输入;当n为0时结束。对输入字符串进行处理,输出在所输入的n个字符串中所输入字符串次数最多的字符串。
解题思路:
使用一个二维数组来存放字符串,先使用一个媒介字符数组,来存放当前输入的字符串,并使用另外一个数组来存放对应字符的个数。每次输入字符串就对其进行查找,然后将出现次数最多的字符串输出。
AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
char color[1010][16];       //存放各颜色字符串 
int a[1010];                //表示各字符的个数 
char b[16];                 //字符媒介 
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        memset(a,0,sizeof(a));  
        for(int j,i=0;i<n;i++)
        {
            scanf("%s",b);
            a[i]=1;             //输入字符个数为1 
            for(j=0;j<i;j++)    //从0至i查找是否具有相同的字符串 
            {
                if(strcmp(color[j],b)==0)
                {
                    a[j]++;
                    break;      //若有相同的,则该字符串第一次出现的对应下标自加一次 
                }
            }
            if(j==i)            //只有不重合的字符串才放到新的数组中 
                strcpy(color[i],b);
        }
        int max=a[0];
        int num=0;
        for(int i=1;i<n;i++)    //查找各种字符对应下标最大值,即各字符出现最多的次数 
        {
            if(max<a[i])
            {
                max=a[i];
                num=i;
            }
        }
        printf("%s\n",color[num]);//将出现次数最多的字符输出 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值