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 pink1.map容器:#include <iostream> #include <cstdio> #include <string> #include <map> using namespace std; int main () { int n; while(cin>>n) { if(n==0) break; map<string,int>s;//设置一个map; string str; for(int i=1;i<=n;i++) { cin>>str; s[str]++; //cout<<s[str]<<endl; } int max=-1; string ans; map<string,int>::iterator it;//设置一个迭代器; for(it=s.begin();it!=s.end();it++) { if(max<it->second)//int类型; { ans=it->first;//string类型; max=it->second; } } cout<<ans<<endl; } return 0; }
2.字符串:#include <iostream> #include <cstdio> #include <cstring> using namespace std; int main() { int n; int i; char ball[1000][10],color[10]; //分别存储现有不同颜色种类和暂时存储本次输入颜色 int num[1000],colors; //分别记录每种颜色出现次数和颜色种数 while(cin>>n) { if(n==0) break; colors=0; //数据初始化 memset(num,0,sizeof(num)); while(n--) { cin>>color; for(i=0; i<colors; i++)//对已有颜色种类做对比 if(!strcmp(color,ball[i]))//匹配成功,颜色种类计数变量增加 num[i]++; if(i==colors)//匹配失败,颜色种类增加,颜色存储,数量增加 { strcpy(ball[colors++],color); num[colors-1]++; //cout<<num[colors-1]<<endl; } } int max=0,j; for(i=0; i<colors; i++) //查找已有颜色的数量最大值的下标 { if(num[i]>max) { max=num[i]; j=i; } } cout<<ball[j]<<endl; } return 0; }