Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90389 Accepted Submission(s): 34336
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 | We have carefully selected several similar problems for you: 1005 1009 1019 1021 1012
题目分析:
题目意思已经很明白了,就是输出出现次数最多的单词
第一种写法 map
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<map> using namespace std; int main() { int n; while(cin>>n,n) { string a; map<string,int>st;// 定义map容器类型 while(n--) { cin>>a; st[a]++;//这里如果 a 出现一次就加1 记录每个单词的次数 首先要明白 map 容器存储是不重复的 } int max=0; map<string,int>::iterator it; // 定义map 指针 就相当于 int类型的 int i; 的定义 for(it=st.begin();it!=st.end();it++) { if((*it).second>max) max=(*it).second; } // 这里就是遍历一下出现次数最多是多少 for(it=st.begin();it!=st.end();it++) { if((*it).second==max) // 这里就是查 次数最多单词的位置 (*it).second 也可以写成 it->second cout<<(*it).first<<endl; } } return 0; }
第二种简单模拟
#include<stdio.h> #include<string.h> int main() { char a[1005][20]; int i,j,n; int b[1005]; while(scanf("%d",&n)&&(n!=0)) { memset(b,0,sizeof(b)); for(i=0; i<n; i++) { scanf("%s",a[i]); } for(i=0; i<n; i++) for(j=0; j<n; j++) { if(strcmp(a[i],a[j])==0) b[i]++; } int max=-1; int k; for(i=0; i<n; i++) { if(max<b[i]) { max=b[i]; k=i; } }//找出出现颜色最多的下标 printf("%s\n",a[k]); } return 0; }