• 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;
    }
  • 相关阅读:
    学习C#泛型
    css半透明渐变过渡效果
    CSS Grid布局
    CSS writing-mode属性
    单点登录原理与简单实现
    linux磁盘挂载
    Fiddler抓包工具学习
    javascript事件机制之冒泡、捕获、传播、委托
    HTML行内元素、块状元素、行内块状元素的区别
    console.log详细介绍
  • 原文地址:https://www.cnblogs.com/rolight/p/3606358.html
Copyright © 2020-2023  润新知