UVa 136 Ugly Number

来源:互联网 发布:sql中update的用法 编辑:程序博客网 时间:2024/06/05 09:50

题目:点击打开链接


思路:用一个优先队列存储,每一次弹出一个最小的整数,分别乘2乘3乘5丢进优先队列,当弹出第1500个时打印。


代码:

#include<cstdio>#include<iostream>#include<queue>#include<map>using namespace std;int main(){priority_queue<long long> a;map<int,bool> b;a.push(-1);b[1]=true;int sum=1;int j=0;while(1){j++;long long x=a.top();a.pop();if(j==1500){cout<<"The 1500'th ugly number is "<<0-x<<"."<<endl;break;}if(!b.count(2*x)){sum++;a.push(2*x);b[2*x]=true;}if(!b.count(3*x)){sum++;a.push(3*x);b[3*x]=true;}if(!b.count(5*x)){sum++;a.push(5*x);b[5*x]=true;}}return 0;}




0 0
原创粉丝点击