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

杭电1058——Humble Numbers(丑数问题)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058任何一个数分解到最后都是由一些质数或者1相乘。基于此,题目中将一个数的质数因子在2,3,5,7之中...
  • qianchangdiyin
  • qianchangdiyin
  • 2016年03月03日 12:56
  • 1735

HDU Humble Numbers (dp)

Humble Numbers Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot...
  • zp___waj
  • zp___waj
  • 2015年06月14日 22:22
  • 996

USACO : Humble Numbers (丑数)

Humble Numbers For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all ...
  • youzn99
  • youzn99
  • 2016年01月27日 14:33
  • 245

USACO 3.1 Humble Numbers (humble)

/* usaco official solution We compute the first n humble numbers in the "hum" array. For simplicity ...
  • u010114214
  • u010114214
  • 2014年02月26日 13:42
  • 1410

Humble Numbers(滚动数组求解)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 yuan
  • Enjoying_Science
  • Enjoying_Science
  • 2014年08月15日 13:44
  • 711

USACO3.1.3 Humble Numbers (humble)

已经知道前k个丑数,如何得到第k+1个丑数? 对于每个质数p,找到最小的丑数h,使得h*p比上一个丑数大,所有的h*p中最小的就是下一个丑数。 但这样会很慢,所以需要一个pindex数组记录每个质...
  • u011265346
  • u011265346
  • 2014年12月10日 09:01
  • 200

Humble Numbers

题意:有一个长度为K的质数集S = {p1, p2, p3, ... , pk},一个整数被定义为humble number当且仅当这个数的因子都是S中的数,求第N个humble number。 ...
  • zc0074200
  • zc0074200
  • 2013年10月16日 18:35
  • 189

humble numbers

Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The...
  • sinat_37668729
  • sinat_37668729
  • 2017年07月17日 16:37
  • 64

modest vs humble

 Modest describes the personality trait or behaviour of not flaunting oneself, talking oneself up...
  • gentle_wolf
  • gentle_wolf
  • 2014年02月27日 22:31
  • 1408

fjnu 1836 Humble Numbers

DescriptionA number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence ...
  • killua_hzl
  • killua_hzl
  • 2008年02月28日 23:28
  • 630
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Humble Numbers
举报原因:
原因补充:

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