hdu 1058 Humble Numbers【dp】

本文介绍了如何通过枚举的方法解决HumbleNumber问题,即找到第n个只有2,3,5,7素因子的数。

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1058

题意:数字只有2,3,5,7素因子的数叫做Humble Number,问你第 n 个Humble Number是什么?

解法:枚举。

代码:

#include <stdio.h>
#include <string.h>
#include <vector>  
#include <string>  
#include <algorithm>  
#include <iostream>
#include <iterator>
#include <fstream>
#include <set>
#include <map>
#include <math.h>

using namespace std;

int n;
int p[6000];
int tmp[5] = { 2, 3, 5, 7 };
set<long long> SET;

void init()
{
    p[1] = 1;
    int n = 1, m = 1, k = 1, l = 1;
    for (int i = 2; i <= 5842; i++)
    {
        int a = p[n] * 2;
        int b = p[m] * 3;
        int c = p[k] * 5;
        int d = p[l] * 7;
        int ans = min(a,min(b,min(c,d)));
        if (ans == a) n++;
        if (ans == b) m++;
        if (ans == c) k++;
        if (ans == d) l++;
        p[i] = ans;
    }
}

int main()
{
    init();
    while (scanf("%d",&n)!=EOF && n)
    {
        printf("The %d",n);

        if (n % 100 == 13 || n % 100 == 12 || n % 100 == 11)
            printf("th");
        else if (n % 10 == 1) printf("st");
        else if (n % 10 == 2) printf("nd");
        else if (n % 10 == 3) printf("rd");
        else printf("th");

        printf(" humble number is %d.\n",p[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值