您的位置:首页 > 其它

丑数(Uva 136)

2018-03-12 20:19 281 查看
不能被2,3,5以外的其他素数整除的数,例如:1,2,3,4,5,6,8,9,10,12,15……
假设x是丑数,那么2x,3x,5x也是丑数,由于要求是第多少个,考虑用优先队列。
生成丑数的时候,可能会重复,所以用set检测是否存入优先队列中……#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int coeff[3]={2,3,5};
priority_queue<LL,vector<LL>,greater<LL> >pq;//注意> >之间要有一个空格
set<LL>s;
int main()
{
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<<".\n";
break;
}
for(int j=0;j<3;j++)
{
LL x2=x*coeff[j];
if(!s.count(x2))
{
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: