STL-map-A - Let the Balloon Rise
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的更多相关文章
- 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 ...
- 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 ...
- HDU1004 Let the Balloon Rise(map的简单用法)
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Let the Balloon Rise(map)
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 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 ...
- 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 ...
- STL: HDU1004Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 杭电1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HD1004Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- VBA-FileToFileUpdate
Public Sub FileToFileUpdate(ByVal fileName As String, ByVal strFrm As String, ByVal strTo As String) ...
- Docker Stack 学习笔记
该文为<深入浅出Docker>的学习笔记,感谢查看,如有错误,欢迎指正 一.简介 Docker Stack 是为了解决大规模场景下的多服务部署和管理,提供了期望状态,滚动升级,简单易用,扩 ...
- linux | 一次网卡故障处理
问题 在centos7系统中,设置ifcfg-eth*文件时,总会纠结NAME和DEVICE到底写哪个或哪个真实生效.这里实例演示下 这是网卡ifcfg-eth4配置文件.没写DEVICE,用的NAM ...
- excel 名次
RANK.AVG 函数 全部显示 全部隐藏 返回一个数字在数字列表中的排位:数字的排位是其大小与列表中其他值的比值:如果多个值具有相同的排位,则将返回平均排位. 语法 RANK.AVG(number, ...
- pyqt5加载pdf文档失败
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog from PyPDF2 import Pdf ...
- day8 编码2
#!/usr/bin/env python # -*- coding:utf-8 -*- #str --->byte encode 编码 s = '二哥' b = s.encode('utf-8 ...
- Android_Service的一些零散知识点_1
service与线程不甚相同,service是Android提供的可供一个允许常驻后台的组件. 可通过StartService()启动Service和BindService()启动Service St ...
- 【WPF学习】第一章 XAML介绍
XAML(Extensible Application Markup Language的简写,发音为“zammel”)是用于实例化.NET对象的标记语言.尽管XAML是一种应用于诸多不同问题领域的技术 ...
- Prometheus的查询语法是PromQL基本语法
PromQL(Prometheus Query Language)是 Prometheus 自己开发的表达式语言,语言表现力很丰富,内置函数也很多.使用它可以对时序数据进行筛选和聚合. 1- Prom ...
- P2710 数列[fhq treap]
调了一辈子的fhq treap- 如果不会最大子段和 如果不会fhq treap 7个操作- 其中三个查询 单点查询其实可以和区间查询写成一个( fhq treap 的修改操作大概就是 \(split ...