杭电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 ...
随机推荐
- scrapy 框架基本使用
scrapy简介: Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 ...
- Hadoop中的java基本类型的序列化封装类
Hadoop将很多Writable类归入org.apache.hadoop.io包中,在这些类中,比较重要的有Java基本类.Text.Writable集合.ObjectWritable等,重点介绍J ...
- jquery点击添加样式,再次点击移除样式
$("#divSetting").on("click", function () { $(this).toggleClass("open") ...
- 【二叉搜索树】的详细实现(C++)
二叉搜索树的概念 从前面讨论折半搜索的性能中可知,如果每次从搜索序列的中间进行搜索,把区间缩小一半,通过有限次迭代,很快就能通近到所要寻找的元素.进一步,如果我们直接输入搜索序列,构造出类似于折半搜索 ...
- appium---切换webview时报错
在上一篇中简单介绍了如何查看webview和切换到webview的方法,可能第一次切换webview的时候会报错“Error: session not created exception: Chrom ...
- Sql 中获取年月日时分秒的函数
getdate():获取系统当前时间 dateadd(datepart,number,date):计算在一个时间的基础上增加一个时间后的新时间值,比如:dateadd(yy,30,getdate()) ...
- c语言thread用法记录。
https://blog.csdn.net/hitwengqi/article/details/8015646 先是c++11之前的 1.最基础,进程同时创建5个线程,各自调用同一个函数 #inclu ...
- layui-table 样式
<!DOCTYPE html> <html> <head> <style> #lay-table { background-color: #fff; c ...
- sqli-libs(46-53关)
Less_46 补充知识:MySQL知识 SQL语句中,asc是指定列按升序排列,desc则是指定列 按降序排列: Select * from users order by 1 desc; 使用降序进 ...
- 概率dp poj 2151
题意: 这道题目的意思很简单,有t个ACM队,m个题目,题目给出了每个队对每个题目做出的概率大小(0到1之间,包含0和1),要求每个队至少做出一道题(签到题),同时,要求获胜队必须至少能够做出n道题( ...