Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 44434 Accepted Submission(s): 15730
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
Author
WU, Jiazhi
Source
ZJCPC2004
Recommend
JGShining
题意: 输出串出现次数最多的那个 数据保证答案只有一个
思路 map数组 直接搞定
我的第一个做法 map 里面用 string int string是对应的串 而 int是串的编号 那么只要对编号对于的数组a 进行加加炒作 然后遍历数组a找到最大的 对应的编号
根据编号找出 string
#include<stdio.h>
#include<map>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main()
{
int a[1005];
int n,i,j,cnt,pos,max;
string b;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
memset(a,0,sizeof(a));
map<string,int>mp;
map<string,int >::iterator it;
if(!n) break;
while(n--)
{
cin>>b;
it=mp.find(b);
if(it!=mp.end())
{
pos=it->second;
a[pos]++;
}
else
{
cnt++;
a[cnt]++;
mp[b]=cnt;
}
}
max=0;pos=0;
for(i=1;i<=cnt;i++)
{
if(max<a[i]) {max=a[i];pos=i;}
}
// printf("max=%d poa=%d\n",max,pos);
for(it=mp.begin();it!=mp.end();it++)
{
if(it->second==pos)
{
cout<<it->first<<endl;
break;
}
}
}
return 0;
}
这个方法比上面的重要
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
int n;
string s;
while(cin>>n&&n)
{
map<string,int> mp;
for (int i=0; i<n; i++) //直接记数
{
cin>>s;
mp[s]++;//我个人感觉 这里指的是 s串对应的i 从原来的变成了i+1 而且自动初始化为0
}
map<string,int>::iterator it;
int max =-1;
for (it=mp.begin();it!=mp.end();it++) //找出最大记数
{
if(it->second>max)
{
max=it->second;
}
}
//printf("%d\n",max);
for(it=mp.begin();it!=mp.end();it++) //最大记数位置
{
if(it->second==max)
{
cout<<it->first<<endl;
}
}
}
return 0;
}