丑数 UVa136
AC的C++程序如下:
#include<iostream>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
int yinzi[3]={2,3,5};
int main()
{
priority_queue<ll,vector<ll>,greater<ll> >q;
set<ll> s;
int ans=0;
q.push(1);
s.insert(1);
while(1)
{
ll n=q.top();
q.pop();
ans++;
if(ans==1500)
{
cout << "The 1500'th ugly number is " << n<<"."<<endl;
break;
}
for(int i=0;i<3;i++)
{
ll m=n*yinzi[i];
if(!s.count(m))
{
q.push(m);
s.insert(m);
}
}
}
}