hdu 1058 Humble Numbers

通过不断的寻找2^a*3^b*5^c*7^d按顺序的数就可以了……

#include <iostream>
#include <cmath>
using namespace std;
__int64
ans[6000];
int
find(int a,int b,int c,int d)
{

    int
aa=a>b?b:a;
    int
bb=c>d?d:c;
    return
aa>bb?bb:aa;
}
   
void
init()
{

    int
a,b,c,d,min,a1,b1,c1,d1;
    a=b=c=d=0;
    ans[0]=1;
    for
(int i=1;i<=5842;i++)
    {

        min=find(a1=ans[a]*2,b1=ans[b]*3,c1=ans[c]*5,d1=ans[d]*7);
        ans[i]=min;
        if
(min==a1)a++;
        if
(min==b1) b++;
        if
(min==c1) c++;
        if
(min==d1) d++;
    }
}

int
main()
{

    int
n;
    init();
    while
(scanf("%d",&n),n)
    {

        if
(n%10==1&&n%100!=11)
            printf("The %dst humble number is %d.\n",n,ans[n-1]);
        else if
(n%10==2&&n%100!=12)
            printf("The %dnd humble number is %d.\n",n,ans[n-1]);
        else if
(n%10==3&&n%100!=13)
            printf("The %drd humble number is %d.\n",n,ans[n-1]);
        else
printf("The %dth humble number is %d.\n",n,ans[n-1]);
    }

    return
0;
}

你可能感兴趣的:(number)