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;
}