zoukankan      html  css  js  c++  java
  • hdu 1058 humble number

    Humble Numbers

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14298    Accepted Submission(s): 6210

    Problem Description
    A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 
    Write a program to find and print the nth element in this sequence
     
    Input
    The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
     
    Output
    For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.
     
    Sample Input
    1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
     
    Sample Output
    The 1st humble number is 1. The 2nd humble number is 2. The 3rd humble number is 3. The 4th humble number is 4. The 11th humble number is 12. The 12th humble number is 14. The 13th humble number is 15. The 21st humble number is 28. The 22nd humble number is 30. The 23rd humble number is 32. The 100th humble number is 450. The 1000th humble number is 385875. The 5842nd humble number is 2000000000.
     
    //思路
    
    //显然每个数都可以由前面某个数*2,*3, *5, *7之后得来 //状态转移方程 dp[i]=min(dp[a]*2,dp[b]*3,dp[c]*5,dp[d]*7) //实践过程 //首先可以想到将前面1到i-1个数一个个乘2,3,5,7,找到最小的,又要比dp[i-1]大的数 //但考虑到dp[i-1]也是这么得来的,假设dp[i-1]=min(dp[a]*2,dp[b]*3,dp[c]*5,dp[d]*7) //若dp[i-1]选中dp[a]*2 //则求解dp[i]时,任何小于等于dp[a]*2的数都不用考虑了 //即 a=a+1 //重复情况 //由此推想到dp[1]的情况,dp[1]=min(dp[0]*2,dp[0]*3,dp[0]*5,dp[0]*7)=2 //dp[2]=min(dp[1]*2,dp[0]*3,dp[0]*5,dp[0]*7)=3 //dp[3]=min(dp[1]*2,dp[1]*3,dp[0]*5,dp[0]*7)=4 //dp[4]=min(dp[2]*2,dp[1]*3,dp[0]*5,dp[0]*7)=5 //dp[5]=min(dp[2]*2,dp[1]*3,dp[1]*5,dp[0]*7)=6 //在dp[5]的时候出现了我们担心的重复情况 //如果不去重的话,按照MIN函数的定义 //dp[6]=min(dp[3]*2,dp[1]*3,dp[1]*5,dp[0]*7)=6 //此时dp[6]就要退回dp[5]; #include <iostream> #include <algorithm> using namespace std; const int maxn = 5850; int dp[maxn]; int MIN( int a,int b,int c,int d ) { int min,e,f; e=a>b?b:a; f=c>d?d:c; min=e>f?f:e; return min; } int main () { int m,a,b,c,d,i; dp[1]=1; a=b=c=d=1; for(i=2;i<=maxn;i++) { dp[i]=MIN(dp[a]*2,dp[b]*3,dp[c]*5,dp[d]*7); if(dp[i]==dp[a]*2) a++; else if(dp[i]==dp[b]*3) b++; else if(dp[i]==dp[c]*5) c++; else if(dp[i]==dp[d]*7) d++; if(dp[i]==dp[i-1]) i--; } while ( cin>>m ) { if ( m==0 ) break; if ( m%10==1 && m%100!=11 ) //111th,211th cout<<"The "<<m<<"st humble number is " <<dp[m]<<". "; else if ( m%10==2 && m%100!=12 ) cout<<"The "<<m<<"nd humble number is " <<dp[m]<<". "; else if ( m%10==3 && m%100!=13 ) cout<<"The "<<m<<"rd humble number is " <<dp[m]<<". "; else cout<<"The "<<m<<"th humble number is " <<dp[m]<<". "; } return 0; }
  • 相关阅读:
    关于c#中的委托和事件
    Unity3d中默认函数调用顺序(MonoBehaviour)
    u3d 摄像机详解
    u3d中的坐标系
    u3d中的向量 vector3 vector2
    u3d中的INput
    C#构造函数
    解析C#中[],List,Array,ArrayList的区别及应用
    Mybatis(七) mybatis的逆向工程的配置详解
    Mybatis(六) Spring整合mybatis
  • 原文地址:https://www.cnblogs.com/neverchanje/p/3489530.html
Copyright © 2011-2022 走看看