POJ 2247

题意:找出所有因子中只有2,3,5,7的数,给出n,求出第n个这样的数

思路:

每个这样的数都可以表示为num2 * num3 * num5 * num7,其中num i 表示i的一个幂

解释得再清楚些,2^mi2 * 3^mi3 * 5^mi5 * 7^mi7

num2, num3, num5, num7分别从1开始,依次记下小于20亿的乘积,最后对这个数组排序。这就是预处理阶段

后面的大家都会了

总结:11th,而不是11st。32ms

 

#include <iostream> #include <algorithm> #define F(i,a,b) for (int i=a;i<=b;i++) #define MAXN 2000000000 using namespace std; int a[6000]; int main() { // init unsigned long long num2, num3, num5, num7, temp; int total = 0, n; num2 = 1; while (num2 <= MAXN ) { num3 = 1; while (num2 * num3 <= MAXN) { num5 = 1; while ( num2 * num3 * num5 <=MAXN ) { num7 = 1; while (num2 * num3 * num5 * num7 <= MAXN ) { a[ ++ total ] = num2 * num3 * num5 * num7; num7 *= 7; } num5 *= 5; } num3 *= 3; } num2 *= 2; } sort(&a[1], &a[total+1] ); while (cin >> n && n) { if (n%10 == 1 && n%100 != 11) printf("The %dst humble number is %d./n", n, a[n] ); else if (n%10 == 2 && n%100 != 12) printf("The %dnd humble number is %d./n", n, a[n] ); else if (n%10 == 3 && n%100 != 13) printf("The %drd humble number is %d./n", n, a[n] ); else printf("The %dth humble number is %d./n", n, a[n] ); } return 0; }

 

你可能感兴趣的:(BI,DST)