HDU 1058 Humble Numbers

这是一道比较简单的动态规划。。。

2           3         4         5          6         7           8            9           10              12               14               15               16

2           3        2*2      5         2*3      7         2*2*2     3*3       2*5           2*2*3          2*7               3*5            2*2*2*2

 

2=min(1*2,    1*3,  1*5,  1*7)

3=min(2*2,    1*3,  1*5, 1*7)

4=min(2*2,    2*3,  1*5, 1*7)

5=min(3*2,    2*3,  1*5, 1*7)

6=min(3*2,    2*3,  2*5, 1*7)

7=min(4*2,    2*3,  2*5, 1*7)

8=min(4*2,    3*3,  2*5, 2*7)

9=min(5*2,    3*3,  2*5, 2*7)

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

 

#include <iostream>
using namespace std;

int min(int a,int b,int c,int d)
{
  int min1,min2;
  min1=a<b?a:b;
  min2=c<d?c:d;
  return min1<min2?min1:min2;
}

int main()
{
 int f[5845], a, b, c, d, n, i;
 f[1] = 1;
 a = b = c = d = 1;
 for(i = 2; i <= 5842; i++)
 {
  f[i]=min(f[a]*2, f[b]*3, f[c]*5, f[d]*7);
  if(f[i]==f[a]*2)
   a++;
  if(f[i]==f[b]*3)
   b++;
  if(f[i]==f[c]*5)
   c++;
  if(f[i]==f[d]*7)
   d++; 
 }
 
 while(cin>>n,n)
 {
 // cout<<f[n]<<endl;
  if(n%10==1&&n%100!=11)
   cout<<"The "<<n<<"st humble number is "<<f[n]<<"."<<endl;
  else if(n%10==2&&n%100!=12)
   cout<<"The "<<n<<"nd humble number is "<<f[n]<<"."<<endl;
  else if(n%10==3&&n%100!=13)
   cout<<"The "<<n<<"rd humble number is "<<f[n]<<"."<<endl;
  else
   cout<<"The "<<n<<"th humble number is "<<f[n]<<"."<<endl;
 }
 return 0;
}

 


/*if(n%10==1&&n%100!=11) 加st
else if(n%10==2&&n%100!=12) 加nd
else if(n%10==3&&n%100!=13) 加rd
else 加th*/

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值