UVa136 Ugly Numbers(优先队列priority_queue)
Ugly Numbers
题目
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included.
Write a program to find and print the 1500'th ugly number.
Input and Output
There is no input to this program. Output should consist of a single line as shown below, with <number> replaced by the number computed.
Sample output
The 1500'th ugly number is <number>.
这道题用到了优先队列。STL的queue头文件中提供了优先队列,用"priority_queue<int> s"方式定义。
用push()和pop()进行元素的入队和出队操作,
top()取队首元素。
priority_queue<int,vector<int>,greater<int> >pq表示越小的整数优先级越大。
看书上的解题的时候,一开始不明白为什么要循环1500次,想了一下,原来是因为每次都取的是最小的元素啊,取上1500次,就是第1500个丑数了。
还有觉得比较神奇的地方是,丑数和丑数相乘,结果依然是丑数。
然后用到了typedef,感觉挺新鲜~
渣渣一枚,坚持!
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
const int coeff[]={,,}; int main(){
priority_queue<LL,vector<LL>,greater<LL> >pq;//优先队列
set<LL> s;//集合
pq.push();
s.insert();
for(int i=;;i++){
LL x=pq.top();
pq.pop();
if(i==){
cout<<"The 1500'th ugly number is "<<x<<".\n";
break;
}
for(int j=;j<;j++){
LL x2=x*coeff[j];
if(!s.count(x2)){
s.insert(x2);
pq.push(x2);
}
}
}
return ;
}
UVa136 Ugly Numbers(优先队列priority_queue)的更多相关文章
- UVA.136 Ugly Numbers (优先队列)
UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...
- UVA136 Ugly Numbers【set】【优先队列】
丑数 丑数是指不能被2,3,5以外的其他素数整除的数.把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15,… 求第1500个丑数. 提示:从小到大生成各个丑数.最小的 ...
- UVA136 Ugly Numbers
题意 PDF 分析 用堆和集合维护即可. 时间复杂度\(O(1500 \log n)\) 代码 #include<iostream> #include<cstdio> #inc ...
- UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...
- Ugly Numbers UVA - 136(优先队列+vector)
Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...
- Ugly Numbers(STL应用)
题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- 丑数(Ugly Numbers, UVa 136)
丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- 【UVA - 136】Ugly Numbers(set)
Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
随机推荐
- Notepad++ 用法技巧
1 搜索技巧 [搜索中文]用正则表达式搜索:[一-龥] 2 用于SWIG语法的模板配置 notepad++是Windows平台上非常优秀的文本编辑器,速度快,功能强,还能自定义语言模板呢.很好用! 这 ...
- ROS topic,service和action的使用场景
参考:ROS中关于topic和service的运用场合 Topics 特点: 1.单向,分工明确,处理连续数据流,topic是一种多对多的形式,一个Node可以订阅多个Topic,可以publish到 ...
- SpringBoot整合MyBatis完成添加用户
怎么创建项目就不说了,可以参考:https://www.cnblogs.com/braveym/p/11321559.html 打开本地的mysql数据库,创建表 CREATE TABLE `user ...
- 使用python连接mysql数据库——pymysql模块的使用
安装pymysql pip install pymysql 使用pymysql 使用数据查询语句 查询一条数据fetchone() from pymysql import * conn = conne ...
- iptables笔记
一.内核转发 *永久开启转发 sysctl -w net.ipv4.ip_forward=1 *查看当前 cat /proc/sys/net/ipv4/ip_forward * 暂时开启 echo 1 ...
- Qt Model/View理解(二)---构造model(细心研读,发现超简单,Model就是做三件事:返回行数量、列数量、data如何显示。然后把model与view联系起来即可,两个例子都是如此)good
数据是一个集合,显示也是一个集合.例如一篇<西游记>的文章,所有的文字就是数据集合,展示方式就是显示的集合,可以以书本的形式,也可以以电纸书的形式,更可以用视频的方式展现. 下面是将一个二 ...
- 区间dp 括号匹配问题
这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区 ...
- 15-Perl 格式化输出
1.Perl 格式化输出Perl 是一个非常强大的文本数据处理语言.Perl 中可以使用 format 来定义一个模板,然后使用 write 按指定模板输出数据.Perl 格式化定义语法格式如下:fo ...
- JS实现带省略号的长分页显示
// 刷新|生成分页信息 function refreshPageInfo(data, pageIndex) { var pageSize = data.pageCount pagingInfo.ht ...
- winform messageBox.Show()
MessageBox.Show(" 5 个参数...... ", " 亮仔提示", MessageBoxButtons.OKCancel, ...