Description
Input
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乘2 3 5 7 组合而来, 所以要求的是组合之后最小的那个数;
状态转移方程就是, 当前的 dp[i] = min4(dp[p2]*2,dp[p3]*3,dp[p5]*5,dp[p7]*7);
p2, p3,p5,p7 分别代表的是2 3 5 7 出现的位置,然后将新得到的数字存入后自加;
AC代码:
#include <iostream> #include <bits/stdc++.h> #define min4(a,b,c,d) min(min(a,b),min(c,d)) using namespace std; int dp[6000] ; int main() { int p2 , p3, p5 , p7; p2=p3=p5=p7= 1 ; dp[1]=1; for(int i = 2 ; i <= 5842;i++) { dp[i] = min4(dp[p2]*2,dp[p3]*3,dp[p5]*5,dp[p7]*7); if(dp[i]==dp[p2]*2) p2++; if(dp[i]==dp[p3]*3) p3++; if(dp[i]==dp[p5]*5) p5++; if(dp[i]==dp[p7]*7) p7++; } int n ; while(cin>>n,n) { if(n%10==1&&n%100!=11) printf("The %dst humble number is %d.\n",n,dp[n]); else if(n%10==2&&n%100!=12) printf("The %dnd humble number is %d.\n",n,dp[n]); else if (n%10==3&&n%100!=13) printf("The %drd humble number is %d.\n",n,dp[n]); else printf("The %dth humble number is %d.\n",n,dp[n]); } }