题目要求
丑数就是这个数的质因子只有2,3,5,7这四个,除此之外不再含有其它
别的质因子。
注意1也被认为是丑数.丑数的前20个为
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, … ;
Input
每行输入一个N,1 <= N <= 5842,N为0时输入结束.
Output
输出相应的第N个丑数.
Sample Input
Raw
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
Sample Output
Raw
1
2
3
4
12
14
15
28
30
32
450
385875
2000000000
这道题在以前,只会常规遍历寻找数字是否整除等于0时,总是显示超时。最近看到了vector,set,map以及优先队列又想起来了。
第一遍做的时候很单纯,丑数是2,3,5,7的倍数,只要从最小的丑数1开始,用一个优先队列保持所有已生成的丑数,每次取出最小的那个数字让它乘上2,3,5,7自然可以得到下一个丑数,因为是优先队列,可以使它从小到大直接排列,要注意的使这样做会出现重复数字,所以要记得去重。最后只要知道所给数字,即可知道该位置上的丑数了。
完整代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<set>
#include<queue>
using namespace std;
typedef long long LL;
const int num[4]={
2,3,5,7