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)的更多相关文章

  1. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

  2. UVA136 Ugly Numbers【set】【优先队列】

    丑数 丑数是指不能被2,3,5以外的其他素数整除的数.把丑数从小到大排列起来,结果如下: 1,2,3,4,5,6,8,9,10,12,15,… 求第1500个丑数. 提示:从小到大生成各个丑数.最小的 ...

  3. UVA136 Ugly Numbers

    题意 PDF 分析 用堆和集合维护即可. 时间复杂度\(O(1500 \log n)\) 代码 #include<iostream> #include<cstdio> #inc ...

  4. 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 ...

  5. Ugly Numbers UVA - 136(优先队列+vector)

    Problem Description Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, ...

  6. Ugly Numbers(STL应用)

    题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  7. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  8. [POJ1338]Ugly Numbers

    [POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

  9. 【UVA - 136】Ugly Numbers(set)

    Ugly Numbers Descriptions: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...

随机推荐

  1. 【Adobe Air程序开发】eclipse安装flash builder 4.7插件以及java、flex整合开发

    看了看网上不少文章,发现很多内容都是很老的,没法用.故把自己的安装过程记录下来,方便以后使用 1.在这里,eclipse使用最新版eclipse juno 3.7 2.在adobe官网https:// ...

  2. 【VS开发】CListBox智能滚动设置

    添加数据以后,再在下面加句, int nCount = m_myListBox.GetCount(); if (nCount > 0) m_myListBox.SetCurSel(nCount ...

  3. Java学习笔记-Java中的常用类

    Java中有很多类是很常用的,此处列举System,Runtime,Date,Calendar,Math System System:类中的方法和属性都是静态的 字段摘要 static PrintSt ...

  4. IntelliJ IDEA 2019.2.1 破解教程, 最新激活码(激活到2089年8月,亲测有效,持续更新中...)

    当前最新版本 IDEA 2019.2.1 本来笔者这边是有个正版激活码可以使用的,但是,2019.9月3号的时候,一些小伙伴反映这个注册码已经失效了,于是拿着自己的 IDEA, 赶快测试了一下,果不其 ...

  5. 15.sqoop数据从mysql里面导入到HDFS里面

    表数据 在mysql中有一个库userdb中三个表:emp, emp_add和emp_contact 表emp id name deg salary dept 1201 gopal manager 5 ...

  6. ARM 版本 瀚高 数据库的启动命令

    1. 在瀚高安装目录下面执行路径 安装目录为: /opt/HighGoDB-4.3.4.3/ bin下./pg_ctl restart -D ../data 本次的密码是: highgo123   2 ...

  7. CentOS7之yum仓库配置

    操作系统版本:CentOS Linux release 7.2.1511 (Core)  Yum软件版本:yum-3.4.3-132.el7.centos.0.1.noarch  Yum主配置文件:/ ...

  8. [官网]mono的官方安装方法

    mono 官方的安装方法 https://www.mono-project.com/download/stable/#download-lin-centos Download Release chan ...

  9. PAT A1011 World Cup Betting(20)

    AC代码 #include <cstdio> #include <algorithm> const int max_n = 3; using namespace std; /* ...

  10. 二项式定理+前缀Sigma

    https://hihocoder.com/problemset/problem/1430 思路: 要用前缀去推Sigma总公式,比较方便.https://blog.csdn.net/weixin_3 ...