题意: 质因数除1外最多只有2,3,5,7的数叫Humble Number,输入n,输出第n个Hunble Number.
解法:预处理出2,3,5,7的幂(2,4,6,8...3,9,27,81...等),放在四个数组中,然后滚动分配2,3,5,7的指数即可(0~...),把INT范围内的都处理出来扔进vector中排序即可.复杂度O_O(就是没甚么复杂的...)
防溢出,最好用long long.
鄙人认为,本题的难点不在于该方法,而是...复习初中英语姿势嗄[感动] 英语中序数乳此搞:如果后两位数是11,12,13那么要接th,否则:最后一位数1st,最后一位是2接nd,3接rd,其它都是th.话说我好多年都没关心过第112用英语怎么说了好么O+_+O
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <climits>
using namespace std;
typedef long long ll;
vector<ll> V;
const int MAX = 50;
ll pow2[MAX] = {1};
ll pow3[MAX] = {1};
ll pow5[MAX] = {1};
ll pow7[MAX] = {1};
int main() {
for (int i = 1; pow2[i-1] < INT_MAX; ++i)
pow2[i] = pow2[i-1]<<1;
for (int i = 1; pow3[i-1] < INT_MAX; ++i)
pow3[i] = pow3[i-1]*3;
for (int i = 1; pow5[i-1] < INT_MAX; ++i)
pow5[i] = pow5[i-1]*5;
for (int i = 1; pow7[i-1] < INT_MAX; ++i)
pow7[i] = pow7[i-1]*7;
//puts("haha");
for (int a = 0; pow2[a] < INT_MAX; ++a) {
for (int b = 0; pow3[b] < INT_MAX; ++b) {
if (pow2[a] * pow3[b] > INT_MAX) break;
for (int c = 0; pow5[c] < INT_MAX; ++c) {
if (pow2[a] * pow3[b] * pow5[c] > INT_MAX) break;
for (int d = 0; pow7[d] < INT_MAX; ++d) {
if (pow2[a] * pow3[b] * pow5[c] * pow7[d] > INT_MAX) break;
V.push_back(pow2[a] * pow3[b] * pow5[c] * pow7[d]);
}
}
}
}
sort(V.begin(), V.end());
int n;
while (~scanf(" %d", &n) && n) {
printf("The %d", n);
if (n%100 == 11 || n%100 == 12 || n%100 == 13) printf("th");
else if (n%10 == 1) printf("st");
else if (n%10 == 2) printf("nd");
else if (n%10 == 3) printf("rd");
else printf("th");
printf(" humble number is %lld.\n", V[n-1]);
}
return 0;
}