题目描述
如果一个数的素因子只包含2,3,5或7,那么我们把这种数叫做丑数。序列1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24, 25,27...展示了前20个丑数。
请你编程寻找这个序列中的第n个元素。
输入输出格式
输入格式
一行,为一个整数n。(1≤n≤5842)
输出格式
一行,“The nth humble number is number.”。里面的n由输入中的n值替换,“st”,“nd”,“rd”和“th”这些序数结尾的用法参照输出样例。
输入输出样例
输入样例
5842
输出样例
The 5842nd humble number is 2000000000.
题解
直接枚举2,3,5,7能构成的数即可,注意精度问题。
#include <iostream> #include <algorithm> #include <cmath> using namespace std; int n; int a[31 * 20 * 14 * 12 + 1], sa; const int maxn = 2000000000; const int p2 = pow(2, 30), p3 = pow(3, 19), p5 = pow(5, 13), p7 = pow(7, 11); int main() { cin >> n; long long now; for(register long long i = 1; i <= p2; i *= 2) { now = i; for(register long long j = 1; j <= p3; j *= 3) { if(now * j > maxn) break; now *= j; for(register long long k = 1; k <= p5; k *= 5) { if(now * k > maxn) break; now *= k; for(register long long l = 1; l <= p7; l *= 7) { if(now * l > maxn) break; a[++sa] = now * l; } now /= k; } now /= j; } } sort(a + 1, a + sa + 1); cout << "The " << n; if(n % 100 >= 11 && n % 100 <= 13) cout << "th"; else if(n % 10 == 1) cout << "st"; else if(n % 10 == 2) cout << "nd"; else if(n % 10 == 3) cout << "rd"; else cout << "th"; cout << " humble number is " << a[n] << ‘.‘; return 0; }
参考程序
原文地址:https://www.cnblogs.com/kcn999/p/10351479.html
时间: 2024-10-08 16:41:28