Humble Numbers (HDU - 1058,已知质因子打表)

本文介绍了一种求解Humble Number的算法实现,Humble Number是指除了1和本身外,仅能被2、3、5、7整除的数。文章详细解析了算法思路,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.题目链接:

HDU-1058

二.题目大意:

规定因子(1 和 本身除外)只有  2,  3,5,7 的数为 Humble Number.

求第 n 个 Humble Number 的值.

三.分析:

题目给出:a[1] == 1.

然后求 2,3,5,7 的倍数,取最小的数,再让它的倍数++;

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-4
#define PI acos(-1.0)
#define ll long long int
using namespace std;

int a[6000];

void init()
{
    int x2, x3, x5, x7;
    x2 = x3 = x5 = x7 = 1;
    a[1] = 1;
    for(int i = 2; i < 5843; ++i)
    {
        a[i] = min(min(a[x2] * 2, a[x3] * 3), min(a[x5] * 5, a[x7] * 7));
        if(a[i] == a[x2] * 2)
            x2++;
        if(a[i] == a[x3] * 3)///注意这里不能是else if,因为两者可能相等.
            x3++;
        if(a[i] == a[x5] * 5)
            x5++;
        if(a[i] == a[x7] * 7)
            x7++;
    }
}

int main ()
{
    init();
    int n;
    while(scanf("%d", &n) && n)
    {
        if(n % 100 == 11 || n % 100 == 12 || n % 100 == 13)///蒟蒻格式盲区
            printf("The %dth humble number is %d.\n", n, a[n]);
        else if(n % 10 == 1)
            printf("The %dst humble number is %d.\n", n, a[n]);
        else if(n % 10 == 2)
            printf("The %dnd humble number is %d.\n", n, a[n]);
        else if(n % 10 == 3)
            printf("The %drd humble number is %d.\n", n, a[n]);
        else
            printf("The %dth humble number is %d.\n", n, a[n]);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值