hdoj 1058 Humble Numbers(dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1058
st[i]=min{st[a]*2,st[b]*3,st[c]*5,st[d]*7}
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int main() 6 { 7 long int n; 8 long int st[5850]; 9 long int a=1,b=1,c=1,d=1,small,i; 10 st[1]=1; 11 for(i=2;i<5850;i++) 12 { 13 small=st[a]*2; 14 if(small>st[b]*3) 15 small=st[b]*3; 16 if(small>st[c]*5) 17 small=st[c]*5; 18 if(small>st[d]*7) 19 small=st[d]*7; 20 if(small==st[a]*2) 21 a++; 22 if(small==st[b]*3) 23 b++; 24 if(small==st[c]*5) 25 c++; 26 if(small==st[d]*7) 27 d++; 28 st[i]=small; 29 } 30 while(scanf("%ld",&n)) 31 { 32 if(n==0) 33 break; 34 if(n%10==1&&n%100!=11) 35 printf("The %ldst humble number is %ld.\n",n,st[n]); 36 else if(n%10==2&&n%100!=12) 37 printf("The %ldnd humble number is %ld.\n",n,st[n]); 38 else if(n%10==3&&n%100!=13) 39 printf("The %ldrd humble number is %ld.\n",n,st[n]); 40 else 41 printf("The %ldth humble number is %ld.\n",n,st[n]); 42 } 43 return 0; 44 }