SCAU 18005 它不是丑数

Description

“丑数”是指除了质因子2,3,5,不含其它质因子的正整数,例如由小到大前10个“丑数”为
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
非“丑数”的前10个数为
7, 11, 13, 14, 17, 19, 21, 22, 23, 26, ...
现要求编写一个程序,输出指定第几位的非“丑数”。

输入格式

第一行为正整数T(T<=10000), 表示case的数目。
此后T行,每行一个正整数 n (n <= 100000000).

输出格式

每一个n,输出第n个非“丑数”

描述:先求出丑数数组,非丑数就是在两个丑数之间,因此用count计算两个丑数之间非丑数的数量并进行累加,直到大于m,然后再减去上一次非丑数的数量,再u[i]+m-count。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <math.h>
using namespace std;
long long u[3000]={1};
void Ugly()
{
    int i=1;
    long long a=0,b=0,c=0;
    long long t2=0,t3=0,t5=0;
    while(i<1200)
    {
        t2=u[a]*2;
        t3=u[b]*3;
        t5=u[c]*5;
        u[i]=min(t2,min(t3,t5));
        if(t2==u[i]) a++;
        if(t3==u[i]) b++;
        if(t5==u[i]) c++;
        i++;
    }
}

int main()
{
    int n,i=0;
    cin>>n;
    Ugly();
    while(n--)
    {
        int m=0;
        cin>>m;
        int count=0;
        while(count<m)
        {
            count =count+(u[i+1]-u[i]-1);
            //cout<<count;
            i++;
        }
        i--;
        count=count-(u[i+1]-u[i]-1);
        cout<<u[i]+m-count<<endl;
        i=0;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值