题目:
丑数是指不能被2,3,5以外的其他素数整除的数,把丑数从小到大排列起来,求第 1500个丑数。。。。
思路:从大到小生成各个丑数。最小的丑数是1,而对于任意丑数 x, x ,
ac代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<iomanip>
#include<set>
#include<sstream>
#define ll long long
using namespace std;
const int coeff[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 %d.\n",x);
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;
}