丑数(Uva 136)
2018-03-12 20:19
281 查看
不能被2,3,5以外的其他素数整除的数,例如:1,2,3,4,5,6,8,9,10,12,15……
假设x是丑数,那么2x,3x,5x也是丑数,由于要求是第多少个,考虑用优先队列。
生成丑数的时候,可能会重复,所以用set检测是否存入优先队列中……#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int coeff[3]={2,3,5};
priority_queue<LL,vector<LL>,greater<LL> >pq;//注意> >之间要有一个空格
set<LL>s;
int main()
{
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<<".\n";
break;
}
for(int j=0;j<3;j++)
{
LL x2=x*coeff[j];
if(!s.count(x2))
{
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}
假设x是丑数,那么2x,3x,5x也是丑数,由于要求是第多少个,考虑用优先队列。
生成丑数的时候,可能会重复,所以用set检测是否存入优先队列中……#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int coeff[3]={2,3,5};
priority_queue<LL,vector<LL>,greater<LL> >pq;//注意> >之间要有一个空格
set<LL>s;
int main()
{
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<<".\n";
break;
}
for(int j=0;j<3;j++)
{
LL x2=x*coeff[j];
if(!s.count(x2))
{
s.insert(x2);
pq.push(x2);
}
}
}
return 0;
}
相关文章推荐
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- HDU 1058||Uva 136 丑数
- UVa 136 丑数
- uva 136 丑数
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- Uva 136 Ugly Numbers(丑数)
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- 丑数 UVA 136
- 丑数 UVa 136
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- e5-7uva136 丑数
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- 紫书章五例题七 丑数 UVa 136(多种方法,在这里介绍优先级队列)
- uva 136 丑数
- UVA136 丑数
- 丑数 uva-136 丑数
- 《算法竞赛入门经典2ndEdition 》例题5-7 丑数(Ugly Numbers, Uva136)
- 5_7 丑数(UVa136)<priority_queue的使用>
- 容器set的应用:丑数 UVA - 136
- UVA 136 丑数