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 Input5
green
red
blue
red
red
3
pink
orange
pink
0Sample 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 ...
随机推荐
- JAVA 增删改查接口命名规范(dao层与 service 层
开发时,有很多规范,这里写的是命名规范. Dao 接口命名 insert batchInsert selectOne selectById count selectList update dele ...
- PR2018制作电子相册
PR2018制作电子相册 新建序列,自定义的参数 重置工作区域的面板 调节声音的大小 剪辑区域小图标的功能介绍 1,点亮状态表示可以自动对齐吸附,方便拼接视频,2,图标表示视频接音频同时选中的,不点亮 ...
- Win10好用的快捷键
Win10好用的快捷键 Win键,Tab键,空格键,上下左右方向键,Enter键,Shift+Tab键 Win键--Tab键--上下左右方向键--Enter确定或者---空格键(确定的意思),Ente ...
- Node中使用MongoDB
简介 MongoDB 中文文档 MongoDB是一个介于关系数据库和非关系数据库(nosql)之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. Mongoose 在Node中可以使用 Mo ...
- centos7 字体库。vim乱码
centos7 字体库.vim乱码 windows上传文件到centos,需要先使用dos2unix命令进行格式转换 先查看/usr/share下有没有这两个文件 没有的话yum -y install ...
- 《手把手教你构建自己的 Linux 系统》学习笔记(10)
目录 /etc/fstab 文件的作用是什么? Linux 内核的图形化启动是怎么回事? Linux 系统中的文件名是否以大小写来进行区别? 「GRUB 中无法找到硬盘」怎么解决? 「GRUB 及配置 ...
- 多柱汉诺塔问题“通解”——c++
多柱汉诺塔问题 绪言 有位同学看到了我的初赛模拟卷上有一道关于汉诺塔的数学题.大概就是要求4柱20盘的最小移动次数. 他的数学很不错,找到了应该怎样推. 如果要把n个盘子移到另一个柱子上,步骤如下: ...
- 【转】为什么使用length获取Java数组的长度
记得vamcily 曾问我:“为什么获取数组的长度用.length(成员变量的形式),而获取String的长度用.length()(成员方法的形式)?” 我当时一听,觉得问得很有道理.做同样一件事情, ...
- Balanced Lineup POJ - 3264
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- 剑指offer-面试题11-旋转数组的最小数字-二分法
/* 题目: 把一个数组最开始的若干个元素搬到数组末尾,我们称之为数组的旋转. 输入一个递增排序的数组的旋转,输出旋转数组中的最小元素. */ /* 思路: 采用二分的方法,旋转数组实质上是上左半部分 ...