题解:对于任意的丑数x,2x,3x,和5x 也都是丑数。通过使用优先队列保存已生成的丑数,每次取出最小的丑数,生成3个新的丑数;对生成的新丑数判重,在保存;
#include<iostream>
#include<queue>
#include<set>
using namespace std;
const int a[3] = {2,3,5};
int main()
{priority_queue<long long, vector<long long>, greater<long long> >pq;set<long long>s;pq.push(1);s.insert(1);for (int i = 1;;i++){long long x1 = pq.top();pq.pop();if(i == 1500){printf("The 1500'th ugly number is %ld.\n",x1);break;}for (int j = 0; j < 3; j++){long long x2 = a[j] * x1;if(!s.count(x2)){s.insert(x2);pq.push(x2);}}}
}