24h購物| | PChome| 登入
2011-12-18 20:19:04| 人氣1,754| 回應0 | 上一篇 | 下一篇

[UVA] 443 - Humble Numbers

推薦 0 收藏 0 轉貼0 訂閱站台

 Humble Numbers 

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 tex2html_wrap_inline35 . 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.



#include<stdio.h>
int main() {
    int DP[5842] = {1}, t2 = 0, t3 = 0, t5 = 0, t7 = 0;
    int tmp, i;
    for(i = 1; i < 5842; i++) {
        while(DP[t2]*2 <= DP[i-1])    t2++;
        while(DP[t3]*3 <= DP[i-1])    t3++;
        while(DP[t5]*5 <= DP[i-1])    t5++;
        while(DP[t7]*7 <= DP[i-1])    t7++;
        tmp = DP[t2]*2;
        if(DP[t3]*3 < tmp)    tmp = DP[t3]*3;
        if(DP[t5]*5 < tmp)    tmp = DP[t5]*5;
        if(DP[t7]*7 < tmp)    tmp = DP[t7]*7;
        DP[i] = tmp;
    }
    int n;
    while(scanf("%d", &n) == 1 && n) {
        printf("The %d", n);
        if(n%10 == 1 && n%100 != 11)
            printf("st");
        else if(n%10 == 2 && n%100 != 12)
            printf("nd");
        else if(n%10 == 3 && n%100 != 13)
            printf("rd");
        else
            printf("th");
        printf(" humble number is %d.\n", DP[n-1]);
    }
    return 0;
}

台長: Morris
人氣(1,754) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 640 - Self Numbers
此分類上一篇:[UVA] 136 - Ugly Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文