136 - Ugly Numbers
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>.
丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下:
1,2,3,4,5,6,8,9,10,12,15,…
求第1500个丑数。
#include <cstdio>
#include <queue>
using namespace std;const int maxNum = 1505;int main() {long long a, b, c;// 优先队列priority_queue<long long, vector<long long>, greater<long long> > pq;// 初始化队列有1这个元素pq.push(1);// 计数long long num = 0;for(int i = 1; i < maxNum; i++) {long long ans = pq.top();// test// printf("%d \t", ans);pq.pop();num++;if(num == 1500) {printf("The 1500'th ugly number is %d.\n", ans);}// 如果ans是ugly number,则2ans,3ans,5ans都是丑数a = ans * 2;b = ans * 3;c = ans * 5;// a 如果与 b或c是同一数的因数关系,那么该数一定在b或c中出现过// 因为b或c比a大if((a % 5) && (a % 3)) {pq.push(a);}// 同理if((b % 5)) {pq.push(b);}pq.push(c);}return 0;
}
136 - Ugly Numbers相关推荐
- UVa 136 - Ugly Numbers
题目:求由1.2.3和5相乘组成的数字的第1500个. 分析:简单题.新的元素一定由原有元素和因子乘积得出.对于每个因子设置一根指向已求得序列的指针.每次取指针对应元素和因子乘积最小的,对应指针后移, ...
- UVA - 136:Ugly Numbers
Ugly Numbers 来源:UVA 标签: 参考资料:<算法竞赛入门经典>P120 相似题目: 题目 Ugly numbers are numbers whose only prime ...
- [POJ1338]Ugly Numbers
[POJ1338]Ugly Numbers 试题描述 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequ ...
- poj 1338 Ugly Numbers(丑数模拟)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem?id=1338 Descr ...
- Poj 1338 Ugly Numbers(数学推导)
一.题目大意 本题要求写出前1500个仅能被2,3,5整除的数. 二.题解 最初的想法是从1开始检验该数是否只能被2,3,5整除,方法是这样的,对于一个数,如果它能被2整除,就除以2,如果它能被3整除 ...
- POJ1338 Ugly Numbers(解法二)【废除!!!】
本文废除!!! 参考链接:POJ1338 Ugly Numbers[水题] 问题链接:POJ1338 Ugly Numbers.基础级练习题,用C语言编写程序. 题意简述:不能被2.3和5以外的素数整 ...
- POJ1338 Ugly Numbers
问题链接:POJ1338 Ugly Numbers.基础级练习题,用C++语言编写程序. 题意简述:不能被2.3和5以外的素数整除的数称为丑数,找出第1500个丑数. 问题分析:换句话说,丑数的因子只 ...
- Ugly Numbers问题
算法一 Ugly Numbers 一.问题描述 质因子只有2.3.5的数称为丑数,为了方便,1也被归为丑数(1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15-) ...
- 【例题5-7 UVA - 136】Ugly Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个丑数x,都能生成3个丑数2x,3x,5x 则我们以1作为起点. 生成丑数. 每次取出set里面最小的那个数. 然后用它去生成其他 ...
最新文章
- Pipe Utilization管道利用率
- ios share extension 真机不显示_ios企业签名:APPGroups实现App之间数据共享
- 腾讯 AI Lab 开源业内最大规模多标签图像数据集
- CVPR 2020 | 利用强化学习进行交互式3D医学图像分割
- python输出csv文件-更高效的Python CSV文件导出
- 用Python解决数据结构与算法问题
- mysql limit耗时过长
- 挖洞技巧:APP手势密码绕过思路总结
- Node mysql mvc vue_net MVC +Vue.js+Element UI 笔记
- 2021-11-17
- 无限磁力_给力的磁力搜索网站你都知道吗?
- 一种基于复制粘贴的cam350邮票孔拼版教程(一)
- 京东移动端分类页面侧导航栏实现
- 什么是OCR?如何使用OCR文字识别软件?
- 开发分布式WebIm--1(设想)
- Linux系统之查找文件和打包压缩文件
- python3安装库报错ERROR: Exception: Traceback (most recent call last): File “A:\ProgramData\Anacon
- 计算机桌面怎么自定义,例举电脑桌面图标后怎么自定义设置呢?
- 搜索算法之爬山法总结和实现
- 用 screen 做串口终端