思路:
从1开始往上*2 *3 *5,记录未出现过的元素。
收获
优先队列,set的find,count查找
#include <iostream>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
const int a[3]={2,3,5};
int main()
{
priority_queue<ll,vector<ll>,greater<ll> >pq;
set<ll>s;
pq.push(1);
s.insert(1);
for(int i=1;;i++)
{
ll x=pq.top();
pq.pop();
if(i==1500)
{
cout<<"The 1500'th ugly number is "<<x<<"."<<endl;
break;
}
for(int j=0;j<3;j++)
{
ll x2=x*a[j];
if(s.find(x2)==s.end())
//if(!s.count(x2))
/*
find()函数返回指向查找元素的迭代器,如果不存在返回set的end()迭代器。
count()返回该元素的个数,返回值为1代表存在,返回值为0表示不存在。
*/
{
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}