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的功能很强大,却还很陌生,要多加积累。

最新文章

  1. 【翻译】MongoDB指南/CRUD操作(三)
  2. 使用jigdo下载debian [windows环境下]
  3. Android 自定义控件(一)
  4. PAT乙级 1032. 挖掘机技术哪家强(20)
  5. yum安装报错有冲突file /usr/lib64/php/modules/fileinfo.so conflicts between
  6. AndroidStudio快捷键汇总
  7. Cocoapod安装 - 管理第三方库
  8. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ
  9. 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
  10. Connect the Cities(prime)
  11. linux、hdfs、hive、hbase经常使用的命令
  12. Python面向对象篇(2)-继承
  13. python web开发-flask中response,cookies,session对象使用详解
  14. React 组件框架
  15. (转)Eclipse中快速输入System.out.println()的快捷键
  16. Best way to learn android and java?
  17. Alpha 答辩总结
  18. HTTP 协议详解(转载)
  19. Camera Sensor
  20. [转].Net Windows服务安装完成后自动启动

热门文章

  1. LeetCode 57. Insert Interval 插入区间 (C++/Java)
  2. Sql Server 2008 【存储过程】 死锁 查询和杀死
  3. 剑指offer-面试题62-圆圈中最后剩下的数字-约瑟夫环-解法2
  4. Java获取IP地址,IpUtils工具类,Java IP地址获取
  5. SpringBoot整合NoSql--(一)Redis
  6. 免费生成二维码接口,可直接嵌入到web项目中,附带嵌入方法,任意颜色二维码,任意大小二维码!
  7. 构造并判断二叉搜索树-js
  8. extern关键字详解
  9. Spark学习之路 (六)Spark Transformation和Action[转]
  10. Java第五节课总结