A - Let the Balloon Rise

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.

InputInput 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.
OutputFor 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
pink
  题目大意:给出n种颜色,要你统计出期中出现次数最多的颜色
  代码一:最原始的办法解决,用双重for循环,每次输入一种颜色都进行一次循环,判断该颜色是否出现过,复杂度O(n

2

).
 #include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==) break; string str[];
cin.get();//!!!!!
for(int i=;i<n;i++)
getline(cin,str[i]); int num[];
int max = ;
for(int i=;i<n;i++){
num[i] = ;
for(int j=i+;j<n;j++){
if(str[i]==str[j])
num[i]++;
}
if(num[i]>num[max]) max = i;
}
cout << str[max] << endl;
}
}

  代码二:使用STL里的map容器

 #include<bits/stdc++.h>
using namespace std; int n;
map<string, int>ballon; int main(){
while(~scanf("%d", &n) && n){
ballon.clear();
cin.get();
for(int i=; i<n; i++){
string colors;
getline(cin, colors);
if(ballon.find(colors) != ballon.end())//该颜色已经出现过
ballon[colors] ++;
else
ballon.insert(map<string, int>::value_type(colors, ));
}
int max = ;
string color;
map<string, int>::iterator it;
for(it = ballon.begin(); it != ballon.end(); it++){
if(it->second > max){
max = it->second;
color = it->first;
}
}
cout << color << endl;
}
}

  代码三:是上段代码的修改,发现无需判断颜色是否出现过,直接插入即可

 #include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std; int main(){
int n;
while(~scanf("%d", &n) && n){
map<string,int>ballon;
string colors;
for(int i=; i<n; i++){
cin >> colors; //用getline(cin,colors)就WA了,这谁能解释
ballon[colors]++;
}
int max = ;
string color;
map<string,int>::iterator it;
for(it=ballon.begin(); it!=ballon.end(); it++){
if(max < it->second){
max = it->second;
color = it->first;
}
}
cout << color << endl;
}
}

  (1)类似题目,要统计某种东西的出现次数时,就可以使用map容器来做,简单又快。

  (2)有个点,在一开始输入气球的颜色时,用cin >> colors就没有问题,而用getline(cin, colors)时就WA了,而解决的办法是在输入n之后,加一个cin.get(),我真的还解释不通这个东西,反正既然colors已经是用string定义的了,就没必要getline了,直接cin即可。

  (3)map的迭代器种,it->first即指的<>种的第一个,second就是第二各咯。

  (4)map的插入ballon.insert(map<string, int>::value_type(colors, 1)),括号里的格式要记住,当然map可能有去重的功能,不需要判断颜色是否出现过,直接ballon[colors]++即可。

  STL的功能很强大,却还很陌生,要多加积累。

STL-map-A - Let the Balloon Rise的更多相关文章

  1. HDU 1004 Let the Balloon Rise【STL<map>】

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. HDU 1004 Let the Balloon Rise map

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. HDU1004 Let the Balloon Rise(map的简单用法)

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  4. Let the Balloon Rise(map)

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. hdu 1004 Let the Balloon Rise strcmp、map、trie树

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  6. HDU 1004 Let the Balloon Rise(map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  7. STL: HDU1004Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. 杭电1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. HD1004Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 最短路径:初涉Dijkstra算法

    模板题目:https://www.luogu.com.cn/problem/P1339 我的代码: #include<cstdio> #include<cstring> #in ...

  2. linux系统挂载u盘拷贝文件

    linux系统在不能远程的情况下用u盘传文件(比如服务器装上系统还没配IP),需要先将u盘挂载到系统中的某个位置,再使用cp命令拷贝文件,简要步骤如下: 1.把U盘插入Linux电脑,确保U盘指示灯是 ...

  3. 【Debian学徒记事】记一次解决Debian开机1min30s

    记一次解决Debian开机1min30s 打开我亲爱的Debian 欸,好像有点慢 [* * * * ] A start job is running for....(*/1min30s) [TIME ...

  4. Java-跳跃路线

    题目: 小明参加了学校的趣味运动会,其中的一个项目是:跳格子.地上画着一些格子,每个格子里写一个字,如下所示: 从我做起振我做起振兴做起振兴中起振兴中华 比赛时,先站在左上角的写着“从”字的格子里,可 ...

  5. mongodb 基础入门教程

    算是学习下来精炼的笔记,希望对大家有帮助.如果有问题欢迎大家指正. 0.概述 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保 ...

  6. CF1300E Water Balance

    题目链接 problem 给出一个长度为n的序列,每次可以选择一个区间\([l,r]\)并将区间\([l,r]\)内的数字全部变为这些数字的平均数.该操作可以进行任意多次. 求出进行任意次操作后可以得 ...

  7. 曼孚科技:AI语音交互领域常用的4个术语

    ​语音交互是基于语音输入的新一代交互模式,比较典型的应用场景是各类语音助手. 本文整理了语音交互领域常用的4个术语,希望可以帮助大家更好地理解这门学科. 1. 语音合成标记语言(SSML) 语音合成标 ...

  8. mybatis第二天02

    MyBatis第二天内容 1.mybatis的执行原理 通过: 1.全局配置文件SqlMapConfig.xml  映射文件mapper.xml 获取得到SqlSessinFactory工厂 2.由工 ...

  9. PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  10. 金蝶云星空BOS开发视频分享

    https://vip.kingdee.com/school/implementCourseOnline