有问题可以反馈一下,谢谢!
#include <iostream>
#include <queue>
#include <vector>
//本体暴力枚举会超时,所以借助stl的优先队列和pair来先求出所有丑数
using namespace std;
typedef long long ll;
typedef pair<ll,ll> type;
long long a[1700];
int main()
{
int t;//case number
//1.先求出所有丑数
int i,n,cnt;
priority_queue<type,vector<type>,greater<type>> p;
type head,temp;
head.first=1,head.second=2;
p.push(head);
for(i=0;i<1691;i++)
{
temp=p.top();
p.pop();
switch(temp.second)//注意switch的使用方法
{
case 2:p.push(make_pair(temp.first*2,2));//如果temp.second==2;则则执行三条语句
case 3:p.push(make_pair(temp.first*3,3));
case 5:p.push(make_pair(temp.first*5,5));
}
a[i]=temp.first;
}
scanf("%d",&t);
while(t--)
{
i=0;
cnt=0;//cnt用来记录非丑数的个数;
scanf("%d",&n);
while(cnt<n)
{
cnt+=a[i+1]-a[i]-1;
i++;
}
i--;
cnt-=a[i+1]-a[i]-1;//此时cnt为a[i]前的非丑数个数;
printf("%lld\n",a[i]+n-cnt);//输出的值细细品一品;
}
}