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 ...
随机推荐
- JavaScript插件编写指南
在编写插件之前,大家要先了解做插件的几个要点: 插件需要满足的条件 一个可复用的插件需要满足以下条件: 插件自身的作用域与用户当前的作用域相互独立,也就是插件内部的私有变量不能影响使用者的环境变量: ...
- 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)
近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...
- Allegro布线基本操作
转:allegro基本步骤 常见问题 cadence16.5中电源线.地线取消飞线显示 目录: 一.Allegro基本技巧 1.关闭电源和地网络的飞线 2.开启特定NET飞线 3.元器件快速对齐(待完 ...
- 解析java.math.BigInteger类——构造函数
最早由于做作业,结识了java的BigInrger类.读着读着,越来越觉得有趣.后来作业做完,也不忍丢下它,索性把全部代码研究一遍. 开始的时候,一个上午时间最多读懂2个方法.但是还是有滋有味的坚持了 ...
- 虚拟机和主机ping不通,SQL Server无法远程连接的解决方法
一.虚拟机网络的配置 这里只列一下自己的配置: 1.编辑---虚拟网络编辑器 进行设置 2.设置对应系统 3.还是Ping不通,最后关闭 虚机内的Windows防火墙,可以Ping通,看来Net模式下 ...
- final、finally、三个关键字的区别
一 final 如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被声明为 abstract的,又被声明为final的.将变量或方法声明为final,可以 ...
- 在Qt Creator中创建C++工程并使用CMake构建项目
创建完毕后,若电脑上没有安装CMake,则无法构建工程, 我用的是绿色版,官网下载地址:https://cmake.org/files/v3.10/cmake-3.10.1-win64-x64.zip ...
- Javascript - ERR_CONTENT_LENGTH_MISMATCH
不知道做了什么,有两天没有跑vue项目啦,今天突然出现加载脚本的时候出现 ERR_CONTENT_LENGTH_MISMATCH这个错误, 所以我去搜索了一下 找到如下答案 http://stac ...
- Strans 内表-xml文件
一.内表到xml文件 <?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transfo ...
- ./autogen.sh: 4: autoreconf: not found
./autogen.sh: 4: autoreconf: not found 是在不同版本的 tslib 下执行 autogen.sh 产生.它们产生的原因一样,是因为没有安装 automake ...