题目大意:丑数的含义是:不能被2,3,5以外的素数整出,现在要求你输出第1500个丑数
解题思路:有优先队列存放丑数,每次取出一个丑数,依次乘上2,3,5,因为是丑数,所以乘上了2,3,5后,依然是丑数,因为它的因子都是都2,3,5组成的.然后再用set判重
#include<cstdio>
#include<queue>
#include<set>
#include<vector>
using namespace std;
set<long long> s;
priority_queue<long long, vector<long long>, greater<long long> > q;
long long mul[3] = {2,3,5};
int main() {
q.push(1);
s.insert(1);
int cnt = 0;
while(1) {
long long tmp = q.top();
q.pop();
cnt++;
if(cnt == 1500) {
printf("The 1500'th ugly number is %lld.\n", tmp);
break;
}
for(int i = 0; i < 3; i++) {
long long t = tmp * mul[i];
if(!s.count(t)) {
s.insert(t);
q.push(t);
}
}
}
return 0;
}