136 - Ugly Numbers
来源:互联网 发布:电脑风扇反转软件 编辑:程序博客网 时间:2024/04/30 05:24
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;}
0 0
- UVa 136 - Ugly Numbers
- 136 - Ugly Numbers
- UVa 136 - Ugly Numbers
- UVa 136 - Ugly Numbers
- 136 - Ugly Numbers
- 136 - Ugly Numbers
- UVA - 136 Ugly Numbers
- UVA 136 Ugly Numbers
- Uva - 136 - Ugly Numbers
- UVA 136 Ugly Numbers
- UVA 136 Ugly Numbers
- UVA - 136 Ugly Numbers
- UVA 136 Ugly Numbers
- UVa 136 Ugly Numbers
- UVa 136 Ugly Numbers
- 136 - Ugly Numbers
- UVA - 136 Ugly Numbers
- Uva 136 - Ugly Numbers
- mybatis日记之mapper参数判断
- docker-codecombat
- ListView解决嵌套冲突
- selenium 学习-js执行,事件模拟,页面加载等待
- leetcode 036 Valid Sudoku
- 136 - Ugly Numbers
- JAVA音乐播放器(二)(实现列表点击播放)
- HDU2069 & UVA 674 Coin Change(换硬币 dp 入门经典水题,背包问题)
- 悼念512汶川大地震遇难同胞——一定要记住我爱你
- 单例模式:创建独一无二的对象
- 高DPI的Win32程序示例
- C++实验5——数组分离
- Linux内存[翻译]
- 高性能阻塞队列