Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 105263 Accepted Submission(s): 40618
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.
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.
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
解题思路:看到这题,想到的就是结构体,里面有2种数据类型,一是颜色string 二是这种颜色出现的次数。然后读入数据,从头遍历 判断颜色的个数是多少。注意在二次循环中j=i+1,因为颜色如果相同的话,前面一定统计过,所以不必j=0遍历;还有maxn保存的是颜色最多的数量,而我们要求的是这种颜色,所以还要保存下这个颜色的index。
AC代码:
#include <cstdio>
#include <cstring>
struct arr{
char color[16];
int times ;
}A[1010];
int main(){
int n ;
while ( scanf("%d",&n) != EOF && n!= 0 ){
memset(A,0,sizeof(A));
for ( int i = 0 ; i < n ; i ++ ){
scanf("%s",A[i].color);
}
for ( int i = 0 ; i < n ; i ++ )
for ( int j = i + 1 ; j < n ; j++ )
{
if(strcmp(A[i].color ,A[j].color) == 0 )
A[i].times ++;
}
int maxn = 0 ,index = 0;
for ( int i = 0 ; i < n ; i ++)
{
if(A[i].times > maxn)
{
maxn = A[i].times;
index = i;
}
}
printf("%s\n",A[index].color);
}
return 0;
}