参考别人的,就是输出第n个丑数。。。
要先打表,丑数:2、3、5、7的乘积
#include <stdio.h>
#define MAX 2000000000
long long a[5842];
int cmp(const void * x, const void *y)
{
if( *(int*)x > *(int*)y ) return 1;
else return -1;
}
int main(void)
{
long long i, j, k, l, t, n, cnt = 0;
for(i = 1; i <= MAX; i *= 2){
for(j = 1; j <= MAX; j*= 3){
if(i*j > MAX) break;
for(k = 1; k <= MAX; k *= 5){
if(i*j*k > MAX) break;
for(l = 1; l <= MAX; l *= 7){
if((t = i*j*k*l) <= MAX){
a[cnt++] = t;
}
}
}
}
}
qsort(a, 5842, sizeof(long long), cmp);
while (scanf("%lld", &n) != EOF && n){
if(n % 10 == 1 && n % 100 != 11)
printf("The %lldst humble number is %lld.\n", n, a[n-1]);
else if(n % 10 == 2 && n % 100 != 12)
printf("The %lldnd humble number is %lld.\n", n, a[n-1]);
else if(n % 10 == 3 && n % 100 != 13)
printf("The %lldrd humble number is %lld.\n", n, a[n-1]);
else printf("The %lldth humble number is %lld.\n", n, a[n-1]);
}
return 0;
}