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): 39524

Problem Description
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.

 
Input
Input 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.

 
Output
For 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,以下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)的更多相关文章

  1. hdoj 1004 学习思路

    hdoj 1004题目大概讲的是,将输入的字符串根据输入次数多少,输出出现次数最多的字符串. 题目逻辑很简单,就是需要选择相应的数据结构,看了别人提交的discuss,明显发现可以使用多种数据结构解这 ...

  2. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  3. HDOJ(~1004)

    T1000 #include <stdio.h> int main() { int a, b; while (scanf("%d %d", &a, &b ...

  4. HDU 1004 MAP【STL__map_的应用】

    强大的MAP,今天终于开始好好学习一次. map内部是用红黑树维持的有序结构. 定义:map<int,string>mapStudent; 查找的时间复杂度为对数级别. 1.构造方法学习两 ...

  5. HDOJ 1004 Let the Balloon Rise (字符串+stl)

    题目: Problem Description Contest time again! How excited it is to see balloons floating around. But t ...

  6. HDOJ 1004题 Let the Balloon Rise strcmp()函数

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  7. string黑科技

    1. string对象的定义和初始化以及读写 string s1; 默认构造函数,s1为空串string s2(s1); 将s2初始化为s1的一个副本string s3("valuee&qu ...

  8. C++STL之string (转)

    在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考. 1:string对象的定义和初始化以及读写 string s1;      默认构造函数,s1为空串 string ...

  9. C++STL之String

    本文直接转载,非原创!仅记录供自己学习之用. 出处:http://blog.csdn.net/y990041769/article/details/8763366 在学习c++STL中的string, ...

  10. TreeMap中文排序,TreeMap倒序输出排列

    1.TreeMap集合倒序排列 import java.util.Comparator; /** * 比较算法的类,比较器 * @author Administrator * */ public cl ...

随机推荐

  1. PA-RISC

    http://baike.baidu.com/view/167703.htm PA-RISC处理器 编辑   HP(惠普)公司的RISC芯片PA-RISC于1986年问世. 第一款芯片的型号为PA-8 ...

  2. 基于HTML,css,jQuery,JavaScript,MySQL搭建博客系统

    一.登陆注册 二.登录验证码相关 三.博客首页显示相关 四.当前用户的家目录显示 五.点赞以及取消 六.父评论以及子评论操作 七.后台管理首页 八.文章的操作(增.删,改) 九.文件上传问题 十.me ...

  3. eclipse下Android工程名称的修改方法

    eclipse下Android工程名称的修改方法 对于已经建立的工程,如果发现原来的工程名不合适,此时若想彻底更改工程名,需要三个步骤: 1.更改工程名 选中工程名,右键-->Refactor- ...

  4. datatable的使用

    学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...

  5. 分布式开源调度框架TBSchedule原理与应用

    主要内容: 第一部分 TBSchedule基本概念及原理 1. 概念介绍 2. 工作原理 3. 源代码分析 4. 与其它开源调度框架对照 第二部分 TBSchedule分布式调度演示样例 1. TBS ...

  6. Scrapy爬虫入门系列1 安装

    安装python2.7 参见CentOS升级python 2.6到2.7 安装pip 参见CentOS安装python setuptools and pip‎ 依赖 https://docs.scra ...

  7. Linux把查询结果写入到文本

    在Linux命令模式下,可以将查询结果写入文件.大概有两种方式,增量写入和覆盖写入. 增量写入: #iostat -m >> /tmp/iostat.txt 覆盖写入: #iostat - ...

  8. 九度OJ 1107:搬水果 (贪心)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5190 解决:1747 题目描述: 在一个果园里,小明已经将所有的水果打了下来,并按水果的不同种类分成了若干堆,小明决定把所有的水果合成一堆 ...

  9. javascript --- 声明提前(学习笔记)

    声明提升 未声明变量 console.log(a); 在没有定义 a 的情况下,直接使用,会报错. 声明变量 console.log(a); var a = 2; 输出结果:undefined 并不会 ...

  10. cenos 6.5 安装apache 2.4.28出现种问题

    编译出错 configure: error: APR-util not found. Please read the documentation 解决办法 wget http://apache.fre ...