zoukankan      html  css  js  c++  java
  • HDU 1058 Humble Numbers 【DP】

    题目链接

    题意

    定义“Humble Numbers”是素因子只含有2,3,5,7的数,求第n个Humble Number是多少。

    分析

    显然直接求出某个范围以内所有的humble Numbers,关键是如何枚举才能保证枚举出来的数是递增的。
    这里用DP来实现,记录当前没有乘以某个因子中的最大数再乘以这个因子得到的数中的最小值,这样说很抽象,看代码:

        while(m<=5842)
        {
            int temp=minn(2*a[b2],3*a[b3],5*a[b5],7*a[b7]);
            a[++m]=temp;
            if(temp==2*a[b2])
                b2++;
            if(temp==3*a[b3])
                b3++;
            if(temp==5*a[b5])
                b5++;
            if(temp==7*a[b7])
                b7++;
        }

    b2,b3,b5,b7最开始都等于1
    这样能够保证每次乘上某个因子过后一定是未求的Humble Numbers中的最小数

    AC代码

    //HDU 1058 Humble Numbers
    //AC 2016-8-8 16:08:11
    //DP
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cctype>
    #include <cstdlib>
    #include <cstring>
    #include <vector>
    #include <set>
    #include <string>
    #include <map>
    #include <queue>
    #include <deque>
    #include <list>
    #include <sstream>
    #include <stack>
    using namespace std;
    
    #define cls(x) memset(x,0,sizeof x)
    #define inf(x) memset(x,0x3f,sizeof x)
    #define neg(x) memset(x,-1,sizeof x)
    #define ninf(x) memset(x,0xc0,sizeof x)
    #define st0(x) memset(x,false,sizeof x)
    #define st1(x) memset(x,true,sizeof x)
    #define INF 0x3f3f3f3f
    #define lowbit(x) x&(-x)
    #define bug cout<<"here"<<endl;
    //#define debug
    
    long long a[6000];
    
    int minn(int a,int b,int c,int d)
    {
        return min(min(a,b),min(c,d));
    }
    
    int main()
    {
        #ifdef debug
            freopen("E:\Documents\code\input.txt","r",stdin);
            freopen("E:\Documents\code\output.txt","w",stdout);
        #endif
        long long temp;
        a[1]=1;
        int n,b2,b3,b5,b7;
        b2=b3=b5=b7=1;
        int m=1;
        while(m<=5842)
        {
            int temp=minn(2*a[b2],3*a[b3],5*a[b5],7*a[b7]);
            a[++m]=temp;
            if(temp==2*a[b2])
                b2++;
            if(temp==3*a[b3])
                b3++;
            if(temp==5*a[b5])
                b5++;
            if(temp==7*a[b7])
                b7++;
        }
        while(cin>>n&&n)
        {
            cout<<"The "<<n;
            if(n%10==1&&n%100!=11)
                cout<<"st";
            else if(n%10==2&&n%100!=12)
                cout<<"nd";
            else if(n%10==3&&n%100!=13)
                cout<<"rd";
            else
                cout<<"th";
            cout<<" humble number is "<<a[n]<<"."<<endl;
        }
        return 0;
    }
  • 相关阅读:
    C语言中字符串常用函数--strcat,strcpy
    linux下core dump【总结】
    C语言memset()函数:将内存的前n个字节设置为特定的值
    const的理解、const指针、指向const的指针
    C99标准的新特性
    ISO C语言新标准(C11)
    哪个版本的gcc才支持c11
    不使用session,借助redis实现验证码
    google浏览器截图工具 Open Screenshot(代码截图)
    springmvc+jpa实现分页的两种方式
  • 原文地址:https://www.cnblogs.com/DrCarlluo/p/6580596.html
Copyright © 2011-2022 走看看