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 ...
随机推荐
- Spring Cloud(十二):Spring Cloud Zuul 限流详解(附源码)(转)
前面已经介绍了很多zuul的功能,本篇继续介绍它的另一大功能.在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选 ...
- xadmin入门使用
,官方文档:http://xadmin.readthedocs.io/en/docs-chinese/views_api.html 中文文档:https://www.kancloud.cn/net_y ...
- hdu5317 RGCDQ 统计
// hdu5317 RGCDQ // // 题目大意: // // 给定一个闭区间[l,r],定义f(x)是x的不同的质因子的个数 // 比方: 12 = 2 * 2 * 3,是两种.所以f(x) ...
- MongoDB 操作手冊CRUD 事务 两步提交
运行两步提交 概述 这部分提供了多记录更新或者多记录事务.使用两步提交来完毕多记录写入的模板. 另外.能够扩展此方法来提供rollback-like功能. 背景 MongoDB对于单条记录的操作是原子 ...
- LeetCode(82)题解: Remove Duplicates from Sorted List II
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题目: Given a sorted linked list, ...
- 对H.264帧类型判断方法
背景描述 我们经常在网络直播推流或者客户端拉流的时候,需要对获取到的H.264视频帧进行判断后处理,我们经常获取到各种不同的视频数据0x67 0x68 0x65 0x61,0x27 0x28 0x25 ...
- ajax json html 结合
<table id="datas" border="1" cellspacing="0" style="border-col ...
- EL表达式 介绍
EL表达式 1.EL简介 1)语法结构 ${expression} 2)[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一 ...
- Qt JSON解析生成笔记
对于这样一段json { "name": "布衣食", "gender": "Male", "age" ...
- ideal 控制台乱码 解决
run config 中 tomcat VM options中填入一下命令 -Dfile.encoding=UTF-8