#include<iostream>
#include<set>
#include<vector>
#include<queue>
using namespace std;
const int coeff[3]={2,3,5};
typedef long long LL;
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‘s ugly numbers is "<<x<<endl;
break;
}
for(int j=0;j<3;j++)
{
LL x2=x*coeff[j];
if(!s.count(x2))
{
s.insert(x2);
pq.push(x2);
}
}
}
}
题目:
丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下:
1,2,3,4,5,6,8,9,10,12,15,。。。
求第1500个丑数
解题思路:
假设x是丑数,则2*x,3*x,5*x都是丑数,因此
用优先队列(升序)存储x,删除x,如是1500个丑数,则退出,否则插入2*x,3*x,5*x,维持当前最小的丑数,用set查重(count功能)
原文:http://www.cnblogs.com/clanguageweaver/p/6710979.html