humble number(hd1058)

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.
 1 /*看题解AC的,结果开始开编译错误了几次,oj说是min()没定义,那为什么自己编译会通过,然后自己写了min(),dp方程 dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))*/
 2 #include<stdio.h>/*dev编译后运行程序中途闪退,提交后却对的*/
 3 using namespace std;
 4 int min(int a,int b)
 5 {
 6     return a>b?b:a;
 7 }
 8 int humble[5843];
 9 void factor()/*马丹看了半天也是似懂非懂,比赛时完全想不出来这么求*/
10 {
11     int post1=1,post2=1,post3=1,post4=1,na,nb,nc,nd,i;
12     humble[1]=1;
13     for(i=2;i<=5843;i++)
14     {
15         humble[i]=min(na=humble[post1]*2,min(nb=humble[post2]*3,min(nc=humble[post3]*5,nd=humble[post4]*7)));
16         if(humble[i]==na) post1++;
17         if(humble[i]==nb) post2++;
18         if(humble[i]==nc) post3++;
19         if(humble[i]==nd) post4++;
20     }
21 }
22 int main()
23 {
24     int n;
25     factor();
26     while(~scanf("%d",&n))
27     {
28         if(n==0)
29             break;
30         printf("The %d",n);
31         if(n%100!=11&&n%10==1)
32             printf("st");
33         else if(n%100!=12&&n%10==2)
34             printf("nd");
35         else if(n%100!=13&&n%10==3)
36             printf("rd");
37         else
38             printf("th");
39         printf(" humble number is %d.\n",humble[n]);
40     }    
41 }

 

posted @ 2015-05-12 22:53  御心飞行  阅读(214)  评论(0编辑  收藏  举报