题目连接:点击打开链接
题目大意:丑数是除去2,3,5之后不能被其它素数整除的数,求出第1500个素数是多少
思路:优先队列,映射应用,运用优先队列将数字按从小到大排列,之后依次乘以2,3,5,求出丑数,用映射判断丑数是否出现过。
代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
int main()
{
int i,j,k,m,n;
int b[3]={2,3,5};
priority_queue<long long,vector<long long>,greater<long long> > q;
map<long long,long long> s;
q.push(1);
s[1]=1;
i=0;
long long x;
while(i<1500)
{
x=q.top();
q.pop();
for(j=0;j<3;j++)
{
long long x1=x*b[j];
if(!s[x1])
{
s[x1]=x1;
q.push(x1);
}
}
i++;
}
printf("The 1500'th ugly number is %lld.\n",x);
return 0;
}