您的位置:首页 > 其它

UVa136 Ugly Numbers(优先队列+set判重)

2017-06-17 22:05 405 查看
//思路:如果x是丑数,那么2*x,3*x,5*x都是丑数,则可以用一个优先队列保存生成的丑数(另外需要判重即相同的丑数不能入队列)

AC源码:

#include <iostream>
#include <set>
#include <vector>
#include <queue>
using namespace std;

const int A[]={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'th ugly number is "<<x<<"."<<endl;
break;
}
for(int k=0;k<3;++k)
{
LL y=x*A[k];
if(!s.count(y))
{
s.insert(y);pq.push(y);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: