蓝桥杯基础练习 a佬吹气球
在19年的致新杯中, a佬tql,又是ak又是first blood,按照每题first blood都能得气球的奖励,a佬各个颜色的气球得了一大堆,a佬想知道得到的哪个颜色的气球最多。
Input
输入包含多个测试用例。每个测试用例都以一个数字N开始(0 < N <= 1000)表示分布的气球的总数。接下来的N行每一行包含一种颜色。气球的颜色最多是由15个小写字母组成的字符串。
N = 0表示结束。
Output
对于每种情况,将最多颜色的气球打印在一行上。保证每个测试用例都有唯一的解决方案。
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<string>
using namespace std;
map<string, int> colors;
int main()
{
int N;
while(scanf("%d",&N)&&N!=0)
{
colors.clear();
string key, p;
int max=0;
for(int i=0;i<N;i++)
{
cin>>key;
colors[key]++;
if(colors[key]>max)
{
max=colors[key];
p=key;
}
}
cout<<p<<endl;
}
return 0;
}
这里用到了map函数,写的代码显得简洁。
map就是从键(key)到值(value)的映射。