转自:http://blog.csdn.net/tukangzheng/article/details/6952244#
Humble Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9452 Accepted Submission(s): 4106
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
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.
Source
Recommend
JGShining
code:
1 /* 2 seq[1]=1 3 seq[2]=2=min(seq[1]*2,seq[1]*3,seq[1]*5,seq[1]*7) 4 seq[3]=3=min(seq[2]*2,seq[1]*3,seq[1]*5,seq[1]*7) 5 seq[4]=4=min(seq[2]*2,seq[2]*3,seq[1]*5,seq[1]*7) 6 seq[5]=5=min(seq[3]*2,seq[2]*3,seq[1]*5,seq[1]*7) 7 seq[6]=6=min(seq[3]*2,seq[2]*3,seq[2]*5,seq[1]*7) 8 seq[7]=7=min(seq[4]*2,seq[3]*3,seq[2]*5,seq[1]*7) 9 seq[8]=8=min(seq[4]*2,seq[3]*3,seq[2]*5,seq[2]*7) 10 seq[9]=9=min(seq[5]*2,seq[3]*3,seq[2]*5,seq[2]*7) 11 seq[10]=10=min(seq[5]*2,seq[4]*3,seq[2]*5,seq[2]*7) 12 seq[11]=12=min(seq[6]*2,seq[4]*3,seq[3]*5,seq[2]*7) 13 seq[12]=14=min(seq[7]*2,seq[5]*3,seq[3]*5,seq[2]*7) 14 seq[13]=15=min(seq[8]*2,seq[5]*3,seq[3]*5,seq[3]*7) 15 seq[14]=16=min(seq[8]*2,seq[6]*3,seq[4]*5,seq[3]*7) 16 seq[15]=18=min(seq[9]*2,seq[6]*3,seq[4]*5,seq[3]*7) 17 seq[16]=20=min(seq[10]*2,seq[7]*3,seq[4]*5,seq[3]*7) 18 seq[17]=21=min(seq[11]*2,seq[7]*3,seq[5]*5,seq[3]*7) 19 seq[18]=22=min(seq[11]*2,seq[8]*3,seq[5]*5,seq[4]*7) 20 */ 21 #include <iostream> 22 #include <iomanip> 23 #include <fstream> 24 #include <sstream> 25 #include <algorithm> 26 #include <string> 27 #include <set> 28 #include <utility> 29 #include <queue> 30 #include <stack> 31 #include <list> 32 #include <vector> 33 #include <cstdio> 34 #include <cstdlib> 35 #include <cstring> 36 #include <cmath> 37 #include <ctime> 38 #include <ctype.h> 39 using namespace std; 40 41 int seq[5843]; 42 43 int min(int a,int b,int c,int d) 44 { 45 int temp1,temp2; 46 temp1=a>b?b:a; 47 temp2=c>d?d:c; 48 return temp1>temp2?temp2:temp1; 49 } 50 51 int main() 52 { 53 int i; 54 int n; 55 seq[1]=1; 56 int a=1,b=1,c=1,d=1; 57 for(i=2;i<=5842;i++) 58 { 59 seq[i]=min(seq[a]*2,seq[b]*3,seq[c]*5,seq[d]*7); 60 if(seq[i]==seq[a]*2) 61 a++; 62 if(seq[i]==seq[b]*3) 63 b++; 64 if(seq[i]==seq[c]*5) 65 c++; 66 if(seq[i]==seq[d]*7) 67 d++; 68 } 69 while(~scanf("%d",&n),n) 70 { 71 if(n%10==1&&n!=11) 72 printf("The %dst humble number is %d.\n",n,seq[n]); 73 else if(n%10==2&&n!=12) 74 printf("The %dnd humble number is %d.\n",n,seq[n]); 75 else if(n%10==3&&n!=13) 76 printf("The %drd humble number is %d.\n",n,seq[n]); 77 else 78 printf("The %dth humble number is %d.\n",n,seq[n]); 79 } 80 return 0; 81 }