InputInput 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.
OutputFor 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
题意:找出颜色出现次数最多的。
思路:可以有很多种方法解答本题,我还用了一个C++封装好了的map.
1 #include<iostream> 2 #include<map> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(cin>>n,n) 8 { 9 map<string,int> p; 10 for(int i=0;i<n;i++) 11 { 12 string s; 13 cin>>s; 14 if(p.count(s)) 15 p[s]++; 16 else 17 p.insert(make_pair(s,1)); 18 } 19 string bigcolor; 20 int max=0; 21 for(map<string,int>::iterator it=p.begin();it!=p.end() ;it++) 22 { 23 if(max<it->second) 24 { 25 max=it->second; 26 bigcolor=it->first; 27 } 28 } 29 cout<<bigcolor<<endl; 30 } 31 return 0; 32 }
原文:https://www.cnblogs.com/yzlzm/p/12600803.html