问第几个非丑数的值…例如第一个是7,第二个是11。
#include <cstdio>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
typedef pair<LL,int> PR;
int n;
LL rep[1530];
void init()//创建一个丑数数组
{
priority_queue<PR,vector<PR>,greater<PR> >Q;
Q.push(make_pair(1,2));
for(int i=1;i<=1500;i++)
{
PR p=Q.top();
Q.pop();
rep[i]=p.first;
switch(p.second)
{
case 2:Q.push(make_pair(p.first*2,2));
case 3:Q.push(make_pair(p.first*3,3));
case 5:Q.push(make_pair(p.first*5,5));
}
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int i=1;
while(rep[i]<=n)//因为数字只有丑数与非丑数。
{
n++;
i++;
}
printf("%d\n",n);
}
return 0;
}
本文探讨了如何确定非丑数序列中特定位置的数值,通过使用优先级队列来高效生成序列并定位目标值。重点在于实现算法以解决实际问题,包括初始化丑数数组、遍历序列直至找到所需的数值。
8126

被折叠的 条评论
为什么被折叠?



