您的位置:首页 > 其它

UVA 136 Ugly Numbers

2017-07-19 21:16 387 查看
题意:丑数是指不能被2,3,5以外的其他素数整除的数。把丑数从小到大排列起来,结果如下:

1,2,3,4,5,6,8,9,10,12,15……

求第1500个丑数。

#include<iostream>
#include<set>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

typedef long long LL;
const int ce[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)
{
printf("The 1500'th ugly number is %I64d.\n",x);
break;
}
for(int j=0;j<3;j++)
{
LL x2=x*ce[j];
if(!s.count(x2)) {s.insert(x2);pq.push(x2);}
}
}
return 0;
}


*优先队列:行为像队列,但先出队列的元素不是先进队列的元素,而是队列中优先级最高的元素,这样就可以允许类似于“急诊病人先插队”的情况。

用 priority_queuepq;来声明。这个pq是一个“整数越大优先级越高的元素”。出队的方法由queue的front()变为了top()。

要实现“数越小优先级越大”的情况,priorioty_queue
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva