#include<set>
#include<cstdio>
#include<queue>
#include<vector> //目前不清楚vector<>的作用
using namespace std;
typedef long long LL;
const int coeff[3]={2,3,5};
const int n=1500;
set <LL> s;
int main(){
priority_queue<LL,vector<LL>,greater<LL> > pq;
pq.push(1);
s.insert(1);
for(int i=1;;i++){
LL tt;
tt=pq.top();
pq.pop();
if (i==n){printf("The 1500'th ugly number is %d.\n",tt);break;}
for(int j=0;j<3;j++){
LL t=tt*coeff[j];
if(!s.count(t)){
s.insert(t);
pq.push(t);
}
}
}
return 0;
}
UVa136(Ugly Numbers)优先队列
最新推荐文章于 2018-07-27 21:25:41 发布