题目大意:
有几个数,他们是2,3,5,7的倍数,把这些数排列出来,顺序输出规定的第几个数
解题思路:
先计算出最前的数,乘2,乘3,乘5,乘7,求出来这里面最小的,把最小的写入数组
代码:
#include <iostream> #include <cstdio> using namespace std; long long dp[5843]; long long min(long long a, long long b, long long c, long long d) { long long temp1 = a < b ? a : b; temp1 = temp1 < c ? temp1 : c; temp1 = temp1 < d ? temp1 : d; return temp1; } int main() { dp[1] = 1; int a = 1, b = 1, c = 1, d = 1; for (int i = 2; i <= 5842; ++i) { dp[i] = min(dp[a] * 2, dp[b] * 3, dp[c] * 5, dp[d] * 7); if (dp[i] == dp[a] * 2) a++; if (dp[i] == dp[b] * 3) b++; if (dp[i] == dp[c] * 5) c++; if (dp[i] == dp[d] * 7) d++; } int n; while (cin>>n&&n) { if (n % 100 != 11 && n % 10 == 1)printf("The %dst humble number is %lld. ", n, dp[n]); else if (n % 100 != 12 && n % 10 == 2)printf("The %dnd humble number is %lld. ", n, dp[n]); else if (n % 100 != 13 && n % 10 == 3)printf("The %drd humble number is %lld. ", n, dp[n]); else printf("The %dth humble number is %lld. ", n, dp[n]); } return 0; }