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 ...
随机推荐
- python urllib2 实现大文件下载
使用urllib2下载并分块copy: # from urllib2 import urlopen # Python 2 from urllib.request import urlopen # Py ...
- pramfs安装
1. 拷贝 linux-3.11 pramfs-1.5.0-3.11 pramfs-Tools 到电脑中(我是新建目录linux中) 2.解压 3.cd pramfs-1.5.0-3.11/ 4.将p ...
- XSS练习平台- https://alf.nu/alert1
https://alf.nu/alert1 参考:https://www.cnblogs.com/renzongxian/p/5617551.html 我目前的进度:https://alf.n ...
- linux内核开源代码地址下载
https://www.kernel.org/pub/linux/kernel/v2.6/
- Java的四层结构dto、dao、service、controller
首先,最底层的就是dto层,dto层就是所谓的model,dto中定义的是实体类,也就是.class文件,该文件中包含实体类的属性和对应属性的get.set方法:<bean> 其次,是da ...
- Spring 循环依赖的三种方式(三级缓存解决Set循环依赖问题)
本篇文章解决以下问题: [1] . Spring循环依赖指的是什么? [2] . Spring能解决哪种情况的循环依赖?不能解决哪种情况? [3] . Spring能解决的循环依赖原理(三级缓存) 一 ...
- Log4j2日志配置详解(1)
log4j与log4j不同:log4j是通过Logger的静态方法getLogger()获取Logger对象,而log4j2是通过LogManager的静态方法getLogger()获取Logger对 ...
- # G++出现cannot open output file … : Permission denied问题
G++出现cannot open output file - : Permission denied问题 这是因为之前的编译运行程序没有退出,导致下一次编译运行无法进行,这应该是命令行下运行才可能出现 ...
- 【Havel 定理】Degree Sequence of Graph G
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2454 [别人博客粘贴过来的] 博客地址:https://www.cnblogs.com/debug ...
- Java8排序
@Data @AllArgsConstructor @NoArgsConstructor public class Apple { private int wight; } 排序 List<In ...