1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
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.
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; int dp[5843] = {1}; int Min(int a, int b, int c, int d) { return min(a, min(b, min(c, d))); } void cal() { int a = 1, b = 1, c = 1, d = 1; dp[1] = 1; for(int i = 1; i <= 5843; i++) { int get = Min(2 * dp[a], 3 * dp[b], 5 * dp[c], 7 * dp[d]); dp[i + 1] = get; if(get == 2 * dp[a]) a++; if(get == 3 * dp[b]) b++; if(get == 5 * dp[c]) c++; if(get == 7 * dp[d]) d++; } } int main() { int n; cal(); while(scanf("%d", &n) != EOF && 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]); } }
HDU 1058 Humble Numbers && NOJ 1420 丑数 (数位dp)
原文:http://blog.csdn.net/tc_to_top/article/details/43524399