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;
}
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;
}
相关文章推荐
- UVa 136 Ugly Numbers【优先队列】
- UVA 136 Ugly Numbers
- UVA 136 & POJ1338 Ugly Numbers
- UVA 136 Ugly Numbers(队列)
- UVA 136 Ugly Numbers
- UVa136(Ugly Numbers)优先队列
- 《算法竞赛入门经典2ndEdition 》例题5-7 丑数(Ugly Numbers, Uva136)
- UVA136 Ugly Numbers
- UVa 136 Ugly Numbers (数论)
- 例题5-7 UVa136 Ugly Numbers(STL:priority_queue)
- UVA 136 Ugly Numbers
- UVA ~ 136 ~ Ugly Numbers (priority_queue + map)
- UVA-136 Ugly Numbers (map容器+优先队列)
- uva136 Ugly numbers
- Ugly Numbers UVA 136 priority_queue+set
- UVA 136(p120)----Ugly Numbers
- UVa 136 Ugly Numbers 【STL】【priority_queue】
- 例题5-7 UVA 136 Ugly Numbers丑数(set+优先队列)
- Ugly Numbers - uva136 - priority_queue与set
- UVa 136 Ugly Numbers(优先队列)