HDU 1058 Humble Number

题意大概是,2,3,5,7的和他们的整数倍数的数都称为Humble Number,将所有的Humble Number从小到大排列,现在输入N(N <= 5842)输出第N个Humble Number

 

考虑到一个一个数是Humble Number,那么他的2,3,5,7倍一定也是HUmble Number,因此只要想办法处理怎么样让其从小到大一个一个输出就好。

 

网上看到一个比较巧妙的办法,在这里记录下来

设f(n)为第n个Humble Number

那么有f(n) =  min(f(l1)*2,f(l2)*3,f(l3)*5,f(l4)*7),初始l1=l2=l3=l4=1

处理完一个数之后相应的值往后挪一个

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

#define min_t(a,b) (((a)<(b))?(a):(b))

LL min(LL a,LL b,LL c,LL d) {
    return min_t(min_t(a,b),min_t(c,d));
} 

LL dp[6000];

void init() {
    int l1 = 1,l2 = 1,l3 = 1,l4 = 1;
    dp[1] = 1;
    for(int i = 2;i <= 5842;i++) {
        dp[i] = min(dp[l1] * 2,dp[l2] * 3,dp[l3] * 5,dp[l4] * 7);
        if(dp[i] == dp[l1] * 2) l1++;
        if(dp[i] == dp[l2] * 3) l2++;
        if(dp[i] == dp[l3] * 5) l3++;
        if(dp[i] == dp[l4] * 7) l4++;
    }
}

int main() {
    int n;
    init();
    while(scanf("%d",&n),n) {
        cout << "The " << n;
        if(n % 100 / 10 == 1) cout << "th";
        else if(n % 10 == 1) cout << "st";
        else if(n % 10 == 2) cout << "nd";
        else if(n % 10 == 3) cout << "rd";
        else cout << "th";
        cout << " humble number is " << dp[n] << "." << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值