Humble Numbers

原创 2006年05月17日 23:34:00
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.05
Run memory: 416K
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Humble Numbers 简单DP

  • 2012-10-08 14:43
  • 49KB
  • 下载

HDU 1058 Humble Numbers && NOJ 1420 丑数 (数位dp)

有趣的数位dp

HDU1058Humble Numbers

Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

ACM--steps--3.2.4--Humble Numbers

Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other...

hdu 1058 Humble Numbers【丑数】

DP&&英语== 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 Humble Numbers Time Limit: 2000/...

hdu Humble Numbers(打表)

Humble Numbers Problem Description A number whose only prime factors are 2,3,5 or 7 is called a ...

hdu 1085 Humble Numbers(打表)

题目:一个数的素数因子是只有有2,3,5,7,叫做humble number,前20个 humble number为 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15...

杭电1058 Humble Numbers

Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • z8110
  • z8110
  • 2015-11-12 15:48
  • 139

USACO-Section 3.1 Humble Numbers(DP)

以前貌似做过类似的一题(hdu-1058),不过只有2,3,5,7四个数字,是这题的简化版 第一反应是用优先队列,每次取队首元素乘以每个素数,压入队列,但这样很可能会MLE 依旧是官方题解:对于每一个...

HDU 1492 The number of divisors(约数) about Humble Numbers

做该题大概要注意以下几方面: ①每个自然数都可分解成质因数相乘的形式 ②数分为三类:质数是不为1,并且因数只为1和其本身的数。和数是不为1且不为质数的数。1既不是质数也非和数。 ③题目虽说输入的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)