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. 利用反射快速给Model实体赋值 使用 Task 简化异步编程 Guid ToString 格式知多少?(GUID 格式) Parallel Programming-实现并行操作的流水线(生产者、消费者) c# 无损高质量压缩图片代码 8种主要排序算法的C#实现 (一) 8种主要排序算法的C#实现 (二)

    试想这样一个业务需求:有一张合同表,由于合同涉及内容比较多所以此表比较庞大,大概有120多个字段.现在合同每一次变更时都需要对合同原始信息进行归档一次,版本号依次递增.那么我们就要新建一张合同历史表, ...

  2. oracle [union.minus.intersect]

    union 两张表的相同字段的数据[记录类型和列数要一致],合并,并且去重 can replace with "in" (但是如果是两个不同的表而且没什么关联的话必须要union了 ...

  3. 升级iOS8和iOS9系统后,保险箱Pro、私人保险箱、私密相冊打开就闪退的官方解决方式

    升级iOS8和iOS9.iOS10系统后,保险箱Pro.私人保险箱.私密相冊打开就闪退的官方解决方式 查看设备iOS操作系统版本号号办法:iPhone/iPad->设置->通用->关 ...

  4. (总结)Nginx配置文件nginx.conf中文具体解释

    #定义Nginx执行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ...

  5. kubernetes对象之deployment

    系列目录 简述 Deployment为Pod和ReplicaSet提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController来方便的管理应用.典型的应 ...

  6. ubuntu 单网卡双 ip

    局域网一套物理网络里有两个 ip 段,单网卡设置多 ip 可实现同时访问两个网段. $ cat /etc/network/interfaces # interfaces(5) file used by ...

  7. 关于CSS和CSS3的布局小知识(干货)

    最近在网站偶然看到的这个网站,进去看了下讲的CSS布局,感觉还不错,讲易懂且实用推荐给大家. http://zh.learnlayout.com/

  8. 【TensorFlow-windows】(二) 实现一个去噪自编码器

    主要内容: 1.自编码器的TensorFlow实现代码(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_6 ...

  9. 防止ViewPager中的Fragment被销毁

    pager.setOffscreenPageLimit(2); 就可以让ViewPager多缓存一个页面

  10. MVC5中使用jQuery Post 二维数组和一维数组到Action

    很久没有写了,最近在做一个MVC项目,这是我做的第一个MVC项目.之前可以说多MVC一点都不了解,今天把昨天遇到的一个问题记录下来.MVC大神就请飘过吧,跟我遇到同样问题的可以进来看看.遇到的第一个问 ...