A - Let the Balloon Rise

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.

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 Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink
  题目大意:给出n种颜色,要你统计出期中出现次数最多的颜色
  代码一:最原始的办法解决,用双重for循环,每次输入一种颜色都进行一次循环,判断该颜色是否出现过,复杂度O(n

2

).
 #include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==) break; string str[];
cin.get();//!!!!!
for(int i=;i<n;i++)
getline(cin,str[i]); int num[];
int max = ;
for(int i=;i<n;i++){
num[i] = ;
for(int j=i+;j<n;j++){
if(str[i]==str[j])
num[i]++;
}
if(num[i]>num[max]) max = i;
}
cout << str[max] << endl;
}
}

  代码二:使用STL里的map容器

 #include<bits/stdc++.h>
using namespace std; int n;
map<string, int>ballon; int main(){
while(~scanf("%d", &n) && n){
ballon.clear();
cin.get();
for(int i=; i<n; i++){
string colors;
getline(cin, colors);
if(ballon.find(colors) != ballon.end())//该颜色已经出现过
ballon[colors] ++;
else
ballon.insert(map<string, int>::value_type(colors, ));
}
int max = ;
string color;
map<string, int>::iterator it;
for(it = ballon.begin(); it != ballon.end(); it++){
if(it->second > max){
max = it->second;
color = it->first;
}
}
cout << color << endl;
}
}

  代码三:是上段代码的修改,发现无需判断颜色是否出现过,直接插入即可

 #include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std; int main(){
int n;
while(~scanf("%d", &n) && n){
map<string,int>ballon;
string colors;
for(int i=; i<n; i++){
cin >> colors; //用getline(cin,colors)就WA了,这谁能解释
ballon[colors]++;
}
int max = ;
string color;
map<string,int>::iterator it;
for(it=ballon.begin(); it!=ballon.end(); it++){
if(max < it->second){
max = it->second;
color = it->first;
}
}
cout << color << endl;
}
}

  (1)类似题目,要统计某种东西的出现次数时,就可以使用map容器来做,简单又快。

  (2)有个点,在一开始输入气球的颜色时,用cin >> colors就没有问题,而用getline(cin, colors)时就WA了,而解决的办法是在输入n之后,加一个cin.get(),我真的还解释不通这个东西,反正既然colors已经是用string定义的了,就没必要getline了,直接cin即可。

  (3)map的迭代器种,it->first即指的<>种的第一个,second就是第二各咯。

  (4)map的插入ballon.insert(map<string, int>::value_type(colors, 1)),括号里的格式要记住,当然map可能有去重的功能,不需要判断颜色是否出现过,直接ballon[colors]++即可。

  STL的功能很强大,却还很陌生,要多加积累。

STL-map-A - Let the Balloon Rise的更多相关文章

  1. HDU 1004 Let the Balloon Rise【STL<map>】

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. HDU 1004 Let the Balloon Rise map

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. HDU1004 Let the Balloon Rise(map的简单用法)

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  4. Let the Balloon Rise(map)

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. hdu 1004 Let the Balloon Rise strcmp、map、trie树

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  6. HDU 1004 Let the Balloon Rise(map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  7. STL: HDU1004Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. 杭电1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. HD1004Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 准备工作-Visual Studio 安装

    说明 网上很多安装教程,等到自己有时间的时候再写一篇自己安装的详细步骤 安装参考(网络) https://blog.csdn.net/qq_33485434/article/details/78454 ...

  2. 如何使用 Vue-TCB 快速在 Vue 应用中接入云开发

    什么是 Vue TCB 我自己平时经常会用到 Vue 来开发前端应用.所以,基于 Vue 的插件系统,封装了一个 Vue 插件. 如何使用 1. 安装 vue-tcb 执行如下命令,安装 vue-tc ...

  3. oracle数据泵导入导出部分用户

    问题描述:需要将140服务器中的tbomnew实例下的部分用户导入到118服务器下的tbompx实例中,本次导入导出的两个数据库均为19C 部分用户名:CORE,MSTDATA,BOMMGMT,CFG ...

  4. 《手把手教你构建自己的 Linux 系统》学习笔记(2)

    Linux 系统定制原理 虚拟根目录 我们的目标是为了完成目标系统(也就是我们要做的系统),他是一个可以独立运行的系统,不依赖雨主系统和临时系统,根据 Linux 系统的特点它需要存放在根目录下面,但 ...

  5. centos7安装opencv3.4.1(同样适用于最新版本4.2.0)

    安装cmake3: echo '[group_kdesig-cmake3_EPEL]name=Copr repo for cmake3_EPEL owned by @kdesigbaseurl=htt ...

  6. MySql优化之存储引擎和sql优化

    存储引擎 使用的存储引擎 myisam / innodb/ memory myisam 存储: 如果表对事务要求不高,同时是以查询和添加为主的,我们考虑使用myisam存储引擎. ,比如 bbs 中的 ...

  7. Hadoop学习之路(8)Yarn资源调度系统详解

    文章目录 1.Yarn介绍 2.Yarn架构 2.1 .ResourceManager 2.2 .ApplicationMaster 2.3 .NodeManager 2.4 .Container 2 ...

  8. 关于 DP 的一些内容

    0.关于         动态规划是编程解题的一种重要手段.1951 年美国数学家 R.Bellman 等人,根据一类多阶段问题的特点,把多阶段决策问题变换为一系列互相联系的单阶段问题,然后逐个加以解 ...

  9. Android记事本在菜单栏添加搜索按钮方法

    效果图 这个app结构和我之前将记事本开发的博客基本一致,我这里直接讲一下怎样添加 使用的开发软件为android studio 首先在res目录下新建文件夹menu,添加目录布局文件main_men ...

  10. 谷歌F12获取接口信息