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
red pink
这道题没什么坑的地方,为什么那么多次不过呢
c语言实现:
#include<stdio.h>
#include<string.h>
int main()
{
while(1)
{
int n;
scanf("%d",&n);
if(n==0)
break;
else
{
int i,num[1005]={0},max,maxi,j;
char an[1005][20]={0};
max=-1;
for(i=0;i<n;i++)
{
scanf("%s",an[i]);
for(j=0;j<i;j++)
{
if(strcmp(an[j],an[i])==0)
{
num[j]++;
if(num[j]>max)
{
max=num[j];
maxi=j;
}
break;
}
}
}
printf("%s\n",an[maxi]);
}
}
return 0;
}
c++实现(string):
#include<iostream>
#include<string>
using namespace std;
int main()
{
while(1)
{
int n;
cin >> n;
if(n==0)
break;
else
{
int i,num[1005]={0},max,maxi,j;
string an[1005];
max=-1;
for(i=0;i<n;i++)
{
cin >> an[i];
for(j=0;j<i;j++)
{
if(an[i]==an[j])
{
num[j]++;
if(num[j]>max)
{
max=num[j];
maxi=j;
}
break;
}
}
}
cout << an[maxi] << endl;
}
}
return 0;
}
c++实现(map):
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
while(1)
{
int n;
cin >> n;
if(n==0)
break;
else
{
int i,num[1005]={0},max;
map <string,int> mp;
max=-1;
for(i=0;i<n;i++)
{
string str;
cin >> str;
mp[str]++;
}
string res;
map <string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
if(it->second>max)
{
max=it->second;
res=it->first;
}
}
cout << res << endl;
}
}
return 0;
}