您的位置:首页 > 其它

Num 12: HDOJ: 题目1004 : Let the Balloon Rise( 字符串问题 )

2015-07-24 13:38 495 查看
原题链接

题目:

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 89088 Accepted Submission(s): 33718



[align=left]Problem Description[/align]

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.

[align=left]Input[/align]

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.

[align=left]Output[/align]

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.

[align=left]Sample Input[/align]

5
green
red
blue
red
red
3
pink
orange
pink
0


[align=left]Sample Output[/align]

red
pink


需要对字符串进行处理,并记录各个字符串出现的次数,和最大数所在的字符串的位置 ( flag );

AC代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int n,max[1001];
char str[1001][100];
while(scanf("%d",&n)&&n)
{
memset(max,0,sizeof(max));
int in,out;
for(in=0;	in<n;	in++)//输入 n 个字符串;
{
scanf("%s",&str[in]);
}
for(in=0;	in<n;	in++)//从第一个字符串开始到最后一个字符串比较是否相同;
for(out=0;	out<n;	out++)//每次都扫描所有的字符串;
{
if(strcmp((str[out]),(str[in]))==0)	max[in]++;//记录每个字符串数目
}
int big=-1,flag;//big用于找出最大的出现次数;flag记录位置;
for(in=0;	in<n;	in++)//寻找最大次数;
{
if(big<max[in])
{
big=max[in];
flag=in;
}
}
printf("%s\n",str[flag]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: