Blog·Tanky WooABOUTRSS

HDOJ 1058 Humble Numbers

29 Jul 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=1058

表示这题不太好理解,但是理解了就好了。

不好说,这题还是分析代码好。

 // Author: Tanky Woo
// HDOJ 1058
#include 
using namespace std;

__int64 humble[6000];
int _min(int a, int b, int c, int d)
{
    int m;
    if(a < b)
        m = a;
    else
        m = b;
    if(m > c)
        m = c;
    if(m > d)
        m = d;
    return m;
}

int main()
{
    int e1=1, e2=1, e3=1, e4=1;
    __int64 a1, a2, a3, a4;
    humble[1] = 1;
    for(int i=2; i<6000; ++i)
    {
        a1 = 2*humble[e1];
        a2 = 3*humble[e2];
        a3 = 5*humble[e3];
        a4 = 7*humble[e4];
        humble[i] =  _min(a1, a2, a3, a4);
        if(humble[i] == a1)
            e1++;
        if(humble[i] == a2)
            e2++;
        if(humble[i] == a3)
            e3++;
        if(humble[i] == a4)
            e4++;
    }

    int n;
    while(scanf("%d", &n;) && n)
    {
        if(n%100==11 || n%100==12 || n%100==13)
            printf("The %dth humble number is %d.\n", n, humble[n]);
        else if(n % 10 == 1)
            printf("The %dst humble number is %d.\n", n, humble[n]);
        else if(n % 10 == 2)
            printf("The %dnd humble number is %d.\n", n, humble[n]);
        else if(n % 10 == 3)
            printf("The %drd humble number is %d.\n", n, humble[n]);
        else
            printf("The %dth humble number is %d.\n", n, humble[n]);
    }
    return 0;
}