利用一个数组 a[N] 来保存自己所需要的数据,对于我们要得到a[i] , 那么a[i] 必然是由前i-1个数中的某一个,乘以2,3,5,7中的某个得到的最小值
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 #define maxn 5842 7 int a[maxn+1]; 8 9 void get_table() 10 { 11 int l1=1 , l2=1 , l3=1 , l4 = 1; 12 a[1] = 1; 13 for(int i = 2 ; i<=maxn ; i++){ 14 int n1 = a[l1] * 2; 15 int n2 = a[l2] * 3; 16 int n3 = a[l3] * 5; 17 int n4 = a[l4] * 7; 18 a[i] = min(min(n1 , n2) , min(n3 , n4)); 19 if(a[i] == n1) l1++; 20 if(a[i] == n2) l2++; 21 if(a[i] == n3) l3++; 22 if(a[i] == n4) l4++; 23 } 24 } 25 26 int main() 27 { 28 get_table(); 29 int n; 30 while(~scanf("%d" , &n)){ 31 if(n == 0) break; 32 if(n % 10 == 1 && n % 100 != 11) 33 printf("The %dst humble number is %d.\n" , n , a[n]); 34 else if(n % 10 == 2 && n % 100 != 12) 35 printf("The %dnd humble number is %d.\n" , n , a[n]); 36 else if(n % 10 == 3 && n % 100 != 13) 37 printf("The %drd humble number is %d.\n" , n , a[n]); 38 else 39 printf("The %dth humble number is %d.\n" , n , a[n]); 40 } 41 return 0; 42 }
我还在坚持,我还未达到我所想,梦~~一直在