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. MSSQL sqlserver 统计"一个字符串"在"另一个字符串"中出现的次数的方法

    转自 http://www.maomao365.com/?p=9858  摘要: 下文讲述sqlserver中最快获取一个字符串在另一个字符串中出现个数的方法分享 实验环境:sql server 20 ...

  2. day17 二分查找

    # 什么叫算法 # 计算的方法 # 99 * 13 = 1287 = 13 * 100 - 13 # 查找 : 找数据 # 排序 : # 最短路径 # 我们学习的算法,都是过去时 # 了解基础的算法, ...

  3. Bringing up interface eth0: Device eth0 does not seem to be presen

    在公司的电脑虚拟机上安装了centos 6.5 ,然后我把他克隆下来用在家里电脑的虚拟机上,打开后查看ip,发现只有回环地址lo,没有eth0, 于是重启网络 输入 service network r ...

  4. ARC 064 F-Rotated Palindromes

    题意 问有多少个长度为 \(N\) 且字符集大小为 \(K\) 的字符串可以通过回文串旋转 (把第一个字符移到最后)若干次得到.\(N,K\le 10^9\) 做法 设\(f_i\)为最小周期为\(i ...

  5. Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释

    这篇文章主要介绍了Pythont特殊语法filter,map,reduce,apply使用方法,需要的朋友可以参考下(1)lambda lambda是Python中一个很有用的语法,它允许你快速定义单 ...

  6. 浅谈python的第三方库——pandas(终)

    作为pandas系列的最终章,本文引出一个数据"复制"问题. 示例如下: 从上图中可以看到:我们对data_pd做了删除一行的操作,但是这并没有改变变量data_pd在内存中的值, ...

  7. mac电脑怎么投屏?教你选择适合自己的Mac投屏软件

    mac上有什么好的投屏软件嘛?苹果手机ios投屏到mac用哪款投屏软件,mac投屏ipad该用哪款软件怎么操作,macdown小编给大家介绍的这几款Mac投屏软件,各有各的特色,总有一款适合你投屏. ...

  8. VSCode常用插件之ESLint使用

    更多VSCode插件使用请访问:VSCode常用插件汇总 ESLint这是VS Code ESLint扩展,将ESLint JavaScript集成到VS Code中. 首先简单说一下使用流程: 1. ...

  9. HTML5 canvas绘图基础(电子名片生成器源码)

    创建canvas <canvas id="myCanvas" class="canvas"> 您的浏览器不支持canvas </canvas& ...

  10. 洛谷【P1595 信封问题】 题解

    题目链接 https://www.luogu.org/problem/P1595 题目描述 某人写了n封信和n个信封,如果所有的信都装错了信封.求所有信都装错信封共有多少种不同情况. 输入格式 一个信 ...