#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int a[3]={2,3,5};
set<LL> s;
priority_queue<LL,vector<LL>,greater<LL> >q;
int main()
{
q.push(1);
s.insert(1);
for(int i=1;i;i++)
{
LL x=q.top();q.pop();
if(i==1500)
{
printf("The 1500'th ugly number is %lld.\n",x);
break;
}
for(int j=0;j<3;j++)
{
LL xx=x*a[j];
if(!s.count(xx))
{
q.push(xx);
s.insert(xx);
}
}
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72
题解:1为丑数,而对于任意丑数x,2x,3x,5x也为丑数,所以可用优先队列每次取出最小的丑数计算生成三个新的丑数,注意用set判重。