HDU1004

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


艾玛,宝宝也开始刷题了[羞愧脸],迟到了三年的刷题经历现在一点点要开始补上了,从最简单最基础的开始,每天一个小小的进步,加油,fighting。
从最开始的讨厌代码,现在也享受起了代码赋予我的满足感,跟着蕊姐共同进步!!!

贴上宝宝的代码咯,虽然是笨笨的,但是毕竟AC了不是:
#include <stdio.h>
int main()
{
    int i=0;                //三个循环控制变量
    int j=0;
    int k=0;
    int n;                  //待接收的字符串个数
    int max=0;              //统计数量最多的字符串
    char ballon[1000][16];  //接收字符串的字符数组
    int num[1000]={0};      //统计各个字符串出现的次数

    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=0;i<1000;i++)                       //清空计数器数组的值
        {
            num[i]=0;
        }
        scanf("%s",ballon[0]);
        num[0]=1;
        for(i=1;i<n;i++)                           //对每一个新输入字符串进行处理
        {
            scanf("%s",ballon[i]);
            for(j=0;j<i;j++)                       //新输入的字符串和之前的字符串进行对比
            {
                for(k=0;ballon[i][k]!='\0'&&ballon[j][k]!='\0';k++)
                {
                    if(ballon[j][k]==ballon[i][k]){}
                    else break;
                }
                if(ballon[i][k]=='\0'&&ballon[j][k]=='\0')    //是之前出现的字符串,在之前的计数器上加一
                {
                  num[j]++;
                  break;
                }
            }
            if(j==i)                                          //是新出现的字符串,在新的计数器上加一
                num[i]++;
        }
        max=0;
        for(i=0;i<n;i++)                                      //找到计数器值最大的数组元素
        {
              // printf("\n%d",num[i]);
           if(num[max]<num[i])
           {
             max=i;
           }
        }
      //  printf("\n最大值是%d",max);
        printf("%s\n",ballon[max]);
    }
}

思路的话我的应该分为两部分:
1、首先是为了应对这种输入应该如何存储,我选择了二维数组来存储多个字符串,由于题目中说明最多1000个字符串,每个字符串长度不超过15,因此开辟的数组空间也就需要1000*16即可,
2、在有了存储好的字符串之后应该如何统计,我选择的方案是在存储了每个字符串之后,就将它和之前的所有字符串进行对比,如果字符串相同则原来元素的计数器加一,如果没有找到之前相同的字符串,则说明是一个新串,需要用一个新的计数器进行统计。
3、用一个变量找到计数器数组中的最大值,然后输出。
4、小细节:
    在每一轮处理之后,要清空计数器数组的值。


感觉自己的水平真是差的不是一点半点,继续加油咯!!!!
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值