zoukankan      html  css  js  c++  java
  • hdu 1058 Humble Numbers

    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.

    URL:http://acm.hdu.edu.cn/showproblem.php?pid=1058

    解题:

    算法分析:典型的DP!

    1 ->?
    1 ->2=min(1*2,1*3,1*5,1*7)
    1 ->2 ->3=min(2*2,1*3,1*5,1*7)
    1 ->2 ->3 -> 4 = min(2*2,2*3,1*5,1*7)
    1 ->2 ->3 -> 4 ->5= min(3*2,2*3,1*5,1*7)
    状态转移方程

    F(n)=min(F(i)*2,F(j)*3,F(k)*5,F(m)*7)(n>i,j,k,m)

    特别的: i,j,k,m 只有在本项被选中后才移动
    code:

    代码一:

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int f[5850];
    int main()
    {
       int i,a, b, c, d;
       a = b = c = d = 1;
       for(i=1i<8i++)
          f[i] = i;
       for(i=8i<5850i++)
       {
          while(2*f[a]<= f[i-1])
            a ++;
          while(3*f[b]<= f[i-1])
            b ++;
          while(5*f[c]<= f[i-1])
            c ++;
          while(7*f[d]<= f[i-1])
           d ++;
         f[i] = min(2*f[a], min(3*f[b], min(5*f[c], 7*f[d])));
    }
    while(cin>>i, i)
    {
        cout<<"The "<<i;
        if(i%10 == 1 && i%100 != 11)
      cout<<"st";
        else if(i%10 == 2 && i%100 != 12)
      cout<<"nd";
        else if(i%10 == 3 && i%100 != 13)
      cout<<"rd";
        else cout<<"th";    
        cout<<" humble number is "<<f[i]<<'.'<<endl;
    }
    return 0;
    }


    代码二:
    #include <iostream>
    #include <string>
    using namespace std;
    const int MaxSize=5844 ;
    __int64 num[MaxSize]={0,1,2,3,4,5,6,7,8}, avai[10]={2,3,5,7},humble[100] ;
    void init()
    {
        int i, j, k ;
         __int64 temp ;
        bool flag ;
        fori=9i<MaxSize++i )
          num[i] = 0x7fffffff ;
        fori=9i<MaxSize++i )
        {
            flag = false ;
            forj=0j<4++j )
            {
                fork=i-1; ; --k )
                {
                    temp = num[k]*avai[j] ;
                    iftemp <= num[i-1] )
                        break ;
                    num[i] = minnum[i], temp ) ;
                    ifnum[i]-num[i-1] == 1 )
                    {
                        flag = true ;
                        break ;
                    }
                }
                ifflag )
                    break ;
            }
        }
    }

    void outputint n )
    {
         printf("The %d", n ) ;
         int last=n%100 ;
         iflast==13 || last==12 || last==11 )
         {
            printf("th humble number is %I64d.\n", num[n] ) ;
            return ;
         }
         last = n%10 ;
         iflast == 1 )
            printf("st") ;
         else iflast == 2 )
            printf("nd") ;
         else iflast == 3 )
            printf("rd");
         else
            printf("th") ;
         printf(" humble number is %I64d.\n", num[n] ) ;
    }

    int main()
    {
        init() ;
        int n ;
        whilescanf("%d", &n), n )
        {
            output( n ) ;
        }
        return 0 ;
    }
  • 相关阅读:
    高德地图API1.4.15较1.4.12的marker属性取值发生变化
    Cesium加载地图提示RangeError:Invalid of array List
    layui分页功能使用——点页码再查询
    设置layui的自定义样式
    页面引入layui.js后提示layui未定义
    SQL语句出现sql关键字
    vs2017 vs2019配置sqlite3连接引擎(驱动)指南(二)vs2019续集
    vs2017 vs2019配置sqlite3连接引擎(驱动)指南(一)vs2019篇
    Qt C++ QDataStream和QBuffer
    Qt C++不规则窗口
  • 原文地址:https://www.cnblogs.com/syxchina/p/2197288.html
Copyright © 2011-2022 走看看