# Humble Numbers

1488人阅读 评论(0)
Time limit: 1 Seconds   Memory limit: 32768K
Total Submit: 1799   Accepted Submit: 660

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence.

### Input Specification

The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.

### Output Specification

For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.

### Sample Input

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0


### Sample Output

The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

### My Solution

#include <stdio.h>
char* GetString(int number){    int value = number % 100;    if(value > 10 && value < 20) return "th";    value = number % 10;    switch(value){    case 1:        return "st";    case 2:        return "nd";    case 3:        return "rd";    default:        return "th";    }}
int main(){    unsigned int Two[50], Three[50], Five[50], Seven[50];    unsigned int product, t1, t2, i, j, k, l;    unsigned int Table[7000];    unsigned int TwoLen, ThreeLen, FiveLen, SevenLen;    int TableLen = 0, m;    i = 1;    Two[0] = Three[0] = Five[0] = Seven[0] = 1;    TwoLen = ThreeLen = FiveLen = SevenLen = 0;    while(1){        Two[i] = Two[i-1] * 2;        if(Two[i] / 2 == Two[i-1]) TwoLen ++;        else break;        i++;    }    i = 1;    while(1){        Three[i] = Three[i-1] * 3;        if(Three[i] / 3 == Three[i-1]) ThreeLen ++;        else break;        i++;    }    i = 1;    while(1){        Five[i] = Five[i-1] * 5;        if(Five[i] / 5 == Five[i-1]) FiveLen ++;        else break;        i++;    }    i = 1;    while(1){        Seven[i] = Seven[i-1] * 7;        if(Seven[i] / 7 == Seven[i-1]) SevenLen ++;        else break;        i++;    }    m = 0;    for(i = 0; i <= SevenLen; i++){        for(j = 0; j <= FiveLen; j++){            t1 = product = Seven[i] * Five[j];            if(product / Seven[i] != Five[j]) break;            for(k = 0; k <= ThreeLen; k++){                product = t1 * Three[k];                if(product / t1 != Three[k]) break;                t2 = product;                for(l = 0; l <= TwoLen; l++){                    product = t2 * Two[l];                    if(product / t2 != Two[l]) break;                    for(m = TableLen-1; m >= 0; m--){                        if(Table[m] > product) Table[m+1] = Table[m];                        else break;                    }                    Table[m+1] = product;                    TableLen++;                }            }        }    }    while(1){        scanf("%d", &m);        if(m == 0) break;        printf("The %d%s humble number is %d./n", m, GetString(m), Table[m-1]);    }    return 0;}
Run time: 00:00.05Run memory: 416K
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：108404次
• 积分：1475
• 等级：
• 排名：千里之外
• 原创：35篇
• 转载：36篇
• 译文：0篇
• 评论：21条
文章存档
最新评论