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): 65317    Accepted Submission(s): 24195


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


//其实就是一个简单的算法题题,就是统计相同字符串出现的次数。然后输出出现次数最多的那个字符串。
首先可以把所有的字符串输入,然后用开头定义好的一个int型统计数组,用来统计每个字符串出现的次数。
但是我却不明不白wa了3次:
WA代码:
#include<stdio.h>
#include<string.h>
int main()
{
    int a[1050]={0},i,j,k,l,sum,n;
    char color[1010][20];
   while( ~scanf("%d",&n))
   {
        memset(a,0,sizeof(a));
       if(n)
       {
            for(i=1;i<=n;i++)
             scanf("%s",color[i]);
            for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
    {
             if(strlen(color[i])==strlen(color[j]))
        {
             for(k=0;k<strlen(color[i]);k++)
             if(color[i][k]!=color[i][k])
              break;
             if(k==strlen(color[i]))
              a[i]++;
        }
    }
          k=1;
          for(i=1;i<=n;i++)
          if(a[k]<a[i])
      k=i;
          printf("%s\n",color[k]);
       }
   }
return 0;
}

测试什么的都没有问题,可就是A不了。

如果大家有什么宝贵建议,请不吝请教可怜


AC代码:
#include <stdio.h>
#include <string.h>
main(){
    int n, i, j, t, max, num[1000];
    char color[1000][16];
    while(scanf("%d", &n) != EOF){
        if(n){
            num[0]=0;
            scanf("%s", color[0]);
            for(i=1; i <n; i++){
                num[i]=0;
                scanf("%s", color[i]);
                for(j=0; j <i-1; j++)
                    if(strcmp(color[i], color[j])==0) num[i] +=1;
            }
            max=num[0];
            t=0;
            for(i=1; i <n; i++)
               if(max <num[i]) {max =num[i]; t=i;}
            printf("%s\n",color[t]);
        }
    }
}

posted on 2014-02-28 16:30  胖胖的乓乓  阅读(150)  评论(0编辑  收藏  举报

导航