HDOJ 1004 Let the Balloon Rise 解题报告

今天早上起来写1004,

看了题目知道是统计字符串的,

没有什么难度,,

意味写一遍就可以AC了。
结果写出来怎么都AC不了。。。

后来上完课回来,想了想,,

才发现是flag没有清零,,

又犯了这种低级错误。。

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 54265    Accepted Submission(s): 19616


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
 

 

Recommend
JGShining
 

代码写得挺短的,

注意下输出格式,就可以了

这个问题最关键的一点就是

用strcmp(s[i],s[j])==0

来确定字符串是否相同,

从而进行统计。

 

比较2个字符串的大小……
s1=s2,strcmp(s1,s2) == 0;
s1>s2, strcmp(s1,s2) == 1;
s1<s2, strcmp(s1,s2) == -1;

 

 

ac代码:

 

#include<stdio.h>
#include<string.h>
int main()
{
    int N, i, j, t1=0,t2=0,flag=0;
    char s[1002][16];          //存放字符串
    while(scanf("%d", &N)!=EOF && N!=0)
    {
        for(i=0;i<N;i++)
        {
            scanf("%s",s[i]);
        }
        for(i=0;i<N;i++)
        {
            t1=0;
            for(j=0;j<N;j++)
            {
                if(strcmp(s[i],s[j])==0)    //统计字符串相同的个数
                    t1++;
            }
            if(t1>t2)
            {
                t2=t1;
                flag=i;            //用来标记最后要输出的字符串的数组下标
            }
        }
    printf("%s\n", s[flag]);
    flag=0;        //不清零的话,,,直接就错了。
    }
    return 0;
}

 

 

posted @ 2013-04-28 12:42  Geekers  阅读(221)  评论(0编辑  收藏  举报