杭电1004 Let the Balloon Rise
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 120507 Accepted Submission(s): 47270
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
green
red
blue
red
red
3
pink
orange
pink
0
#include <iostream>
#include<stdlib.h>
#include<string>
#include<map>
#include<vector>
#include<algorithm> using namespace std; typedef pair<string,size_t> PAIR; //定义排序比较函数,通过value比较
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
return lhs.second > rhs.second;
} int main(void)
{
int n;
map<string,size_t>color_balloon;
string * input_str; while(cin>>n&&n)
{
input_str=new string[n];//为输入的颜色分配空间
for(int i=;i<n;i++)
{
cin>>input_str[i];
++color_balloon[input_str[i]];//提取input_str[]计数器并对其加1
}
//把map中元素转存到vector中
vector <PAIR> color_str_vec(color_balloon.begin(),color_balloon.end());
//按降序排序
sort(color_str_vec.begin(),color_str_vec.end(),cmp_by_value);
//color_str_vec已经按照降序排序,输出第一个即为出现次数最多的一个
cout<<color_str_vec[].first<<endl;
//必须清空
color_balloon.clear();
delete [] input_str;
}
return ;
}
代码中知识参考:
思路供给:http://blog.csdn.net/always2015/article/details/44975799
map的比较函数那个知识,还有pair转化为vec的用法:http://www.cnblogs.com/fengting/p/5847347.html
第二种:
我用两个数组,一个数组用来存放字符串,一个数组用来存放对应字符串的出现次数。然后算法实现基本为:输入一个字符串,我先在之前的存入的字符串中找。如果找到了,那么该字符串对应的num++,如果找不到,那就把这个字符串作为一个新的字符串插入到字符串数组中,并且其num[i]++(从0变为1)。然后就是简单的排序输出。这个思路还是很清晰的。代码如下:
#include <iostream>
#include<math.h>
#include <iomanip>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<list>
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<sstream>
#include<string.h>
using namespace std; int main()
{
int n; while(cin>>n)
{
string input[];//初始化最好在循环里弄 没那么多屁事
for(int u=;u<;u++)
input[u]=" ";
int num[]={};
string temp;
int i,j;
for(i=;i<n;i++)//开始实现算法
{
cin>>temp;//读一个字符串
for(j=;j<i;j++)//在已经输入的 字符串数组中找 有没有一样的
{
if(temp==input[j])
{
num[j]++;//有的话 对应的下标++ !!!!注意 这边是J++ 因为是在已经存在的数组中找
break;
}
}
if(i==j)//找不到 说明 没有 那就是多了一种颜色 那就要把新颜色加入到数组中去
{
input[i]=temp;
num[i]++;//然后别忘记对应的 数字++
}
} int max=;
for(i=;i<n;i++)//排序
{
if(num[i]>num[max])
{
max=i;
}
}
cout<<input[max]<<endl;//输出 } return ;
}
杭电1004 Let the Balloon Rise的更多相关文章
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- 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 ...
- 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 ...
- hdu 1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 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 ...
- 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 ...
- hduoj#1004 -Let the Balloon Rise [链表解法]
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Problem Description Contest time again! How exci ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDU 1004 - Let the Balloon Rise(map 用法样例)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
随机推荐
- Centos7 安装 Cockpit
1sudo yum -y install epel-release sudo yum -y update sudo shutdown -r now 2yum -y install cockpit sy ...
- 使用 navigator.sendBeacon() 上报数据
http://kaifage.com/notes/76/navigator-sendBeacon.html 如某些统计系统,在页面unload时,如果要上报当前数据,采用xhr的同步上报方式,会阻塞当 ...
- EQ实现
原理参考: https://www.cnblogs.com/fellow1988/p/9189338.html https://www.cnblogs.com/fellow1988/p/9136346 ...
- pycharm2019.3安装以及激活
最近很多的pycharm激活过期的,小伙伴们问我pycharm要怎么激活?这里就分享一下pycharm最新版本的安装以及激活吧!!! 首先先去官网(https://www.jetbrains.com/ ...
- 劳动人民万岁(拒绝惰性)------- 浅谈迭代对象(Iterable) 迭代器(Iterator)
一.前戏 问题:如果一次抓取所有城市天气 再显示,显示第一个城市气温时有很高的延时,并且很浪费储存空间 解决方案:以“用时访问”策略,并且能把说有城市气温封装到一个对象里,可用for一句进行迭代 二. ...
- command failed: npm install --loglevel error --registry=https://registry.npm 用vue-cli 4.0 新建项目总是报错
昨天新买的本本,今天布环境,一安装vue-cli发现都4.0+的版本了,没管太多,就开始新建个项目感受哈,一切运行顺利,输入 "vue create app" 的时候,一切貌似进展 ...
- node.js 和 yarn 安装
电脑环境 windows10 专业版 64 位 node.js 安装 Node.js 官方网站下载:https://nodejs.org/en/ 之后一步步得傻瓜式安装 配置环境变量和查看node.j ...
- Python偶斐波那契数
斐波那契数列中的每一项都是前两项的和.由1和2开始生成的斐波那契数列前10项为 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … 考虑该斐波那契数列中不超过四百万的项,求其中为 ...
- C++指针和引用及区别
1.变量 首先最重要的,variable的定义,当你申明一个变量的时候,计算机会将指定的一块内存空间和变量名进行绑定:这个定义很简单,但其实很抽象,例如:int x = 5; 这是一句最简单的变量赋值 ...
- google插件跨域含用户请求WebApi解决的方案
问题描述: google插件跨域请求WebApi相关解决方案 1.ajax解决含登录用户信息 $.ajax({ url: url, type: "POST", timeout: 6 ...