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.
A test case with N = 0 terminates the input and this test case is not to be processed.
5 green red blue red red 3 pink orange pink 0
redpink
题意:输入n再输入n个颜色,输出最多的那一种,用map<string,int>就可以解决
#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<string>
using namespace std;
struct node
{
string s;
}m[1001];//存储颜色
int main()
{
string a;
int n,i,j,k;
while(cin>>n)
{
if(n==0)
{
break;
}
map<string,int>flag;
i=0;
for(k=0;k<n;k++)
{
cin>>a;
if(flag[a]==0)//等于0说明没有出现过
{
flag[a]=1;
m[i].s=a;保存下来
i+=1;
}
else
{
flag[a]+=1;
}
}
int temp=0,k=0;
for(j=0;j<i;j++)
{
if(flag[m[j].s]>temp)//找最大的
{
temp=flag[m[j].s];
k=j;
}
}
cout<<m[k].s<<endl;
}
}