HDOJ-1004(map)
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 103000 Accepted Submission(s): 39524Problem DescriptionContest 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 Input5greenredblueredred3pinkorangepink0Sample Outputredpink
先上题意:
首先输入数据个数n,以下n行输入n个字符串表示气球颜色,输出出现次数最多的颜色。
本题也是做过好久的老题了,上次我们探讨的是用字符串比较函数strcmp()来比较两字符串是否相等从而判断出现次数(见这里)。这次我们用map映照容器来重新做一下这个题目。
用map容器相对来说就要简单一些,每当输入相应的颜色,与之相对应的数值就+1,最后再输出最大数值最大的颜色就好。
附AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std; int main(){
map<string,int> Bollon;//定义map容器
string Color,Maxcolor;
int n;
while(~scanf("%d",&n)&&n!=){
Bollon.clear();//每次处理下一组数据前要清空容器
while(n--){//输入气球颜色,相应颜色气球个数加一
cin>>Color;
Bollon[Color]++;
}
int max=;
map<string,int>::iterator it;//定义前向迭代器,向前遍历容器
for(it=Bollon.begin();it!=Bollon.end();it++){
if(it->second>max){//不知为何换成it.second就编译不过
max=it->second;
Maxcolor=it->first;//找出个数最多的气球颜色
}
}
cout<<Maxcolor<<endl;
}
return ;
}
HDOJ-1004(map)的更多相关文章
- hdoj 1004 学习思路
hdoj 1004题目大概讲的是,将输入的字符串根据输入次数多少,输出出现次数最多的字符串. 题目逻辑很简单,就是需要选择相应的数据结构,看了别人提交的discuss,明显发现可以使用多种数据结构解这 ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDOJ(~1004)
T1000 #include <stdio.h> int main() { int a, b; while (scanf("%d %d", &a, &b ...
- HDU 1004 MAP【STL__map_的应用】
强大的MAP,今天终于开始好好学习一次. map内部是用红黑树维持的有序结构. 定义:map<int,string>mapStudent; 查找的时间复杂度为对数级别. 1.构造方法学习两 ...
- HDOJ 1004 Let the Balloon Rise (字符串+stl)
题目: Problem Description Contest time again! How excited it is to see balloons floating around. But t ...
- HDOJ 1004题 Let the Balloon Rise strcmp()函数
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- string黑科技
1. string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串string s2(s1); 将s2初始化为s1的一个副本string s3("valuee&qu ...
- C++STL之string (转)
在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串 string ...
- C++STL之String
本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...
- TreeMap中文排序,TreeMap倒序输出排列
1.TreeMap集合倒序排列 import java.util.Comparator; /** * 比较算法的类,比较器 * @author Administrator * */ public cl ...
随机推荐
- RTMP协议规范(转载)
译序: 本文是为截至发稿时止最新 Adobe 官方公布的 RTMP 规范.本文包含 RTMP 规范的全部内容.是第一个比较全面的 RTMP 规范的中译本.由于成文时间仓促,加上作者知识面所限,翻译错误 ...
- Jenkins Robot framework 持续集成环境搭建
为什么我们要引入RF?其实最初我们引入RF是为了能够快速的开展自动化验收测试,为敏捷保驾护航.这其中有个重要的工具Jenkins,同时也是应群里朋友们的要求,这次就来介绍一下RF如何快速便捷的结合Je ...
- 实习日记)select option 选择不同的option时, 页面发生不同的变化
怎么在下拉框的选择不同的option时, 页面发生响应的变化 因为option是没有点击事件什么的, 只有select才有, 所以不能通过option的点击事件来完成, 所以开始的尝试都失败了(之前 ...
- JavaScript2种构造函数创建对象的模式以及继承的实现
第一种模式: function Person(){ } Person.prototype.say=function(){ alert('hello'); } var person=new Person ...
- [oracle]pl/sql --分页过程demo
这句sql能够用来查询一张表中的特定位置的记录 --查询的方法获取分页的语句 select *from (select t1.*,rownum rn from (select *from books) ...
- linux安装ssh(转载)
CentOS安装ssh最笨的方法:yum install ssh yum install openssh-server/etc/init.d/sshd status看sshd服务的状态/etc/ini ...
- 【转】IDA远程调试 The debugger could not attach to the selected process. irs_recv 等待的操作过时
IDA连接android_server 选中进程点ok之后 连接不上报错 The debugger could not attach to the selected process. This can ...
- Activity和ListActivity的区别
http://book.51cto.com/art/201007/212051.htm
- 区分:WebElement, MobileElement, AndroidElement, and iosElement
区分:WebElement, MobileElement, AndroidElement, and iosElement 原文地址:https://discuss.appium.io/t/differ ...
- for(String s:list)的运行
源码 List<String> list = new ArrayList<>(); for (String s:list){ } class文件 List<String& ...