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 ;
}

你可能感兴趣的:(number)