Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90656 Accepted Submission(s): 34463
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
Author
WU, Jiazhi
Source
Recommend
水题,运用字符串函数可以很轻松地解决问题,使用了标记的思想。
题意:第一个数字代表气球个数,后面的字符串表示气球的颜色,输出颜色出现次数最多的颜色,结果保证只有一种。
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int main() 6 { 7 char str[1005][20]; 8 int n,m,i,j,s[1005],t; 9 while(~scanf("%d",&n)&&n) 10 { 11 for(i=0; i<n; i++) 12 scanf("%s",str[i]); //输入字符串,用str数组存起来 13 memset(s,0,sizeof(s)); 14 int max=0; //记录最后出现最大的次数 15 for(i=0; i<n; i++) 16 { 17 for(j=i+1; j<n; j++) 18 if(strcmp(str[i],str[j])==0) //strcmp函数比较两个字符串是否相同 19 s[i]++; //标记每种颜色出现的次数 20 if(s[i]>max) 21 { 22 max=s[i]; 23 t=i; //记录出现最大次数的颜色位置 24 } 25 } 26 printf("%s ",str[t]); 27 } 28 }