HDU 1058 humble numbers

本题思路很简单:就是用2,3,5,7循环来求第i个hum[i],第i个hum[i]必定等于前i-1个数中其中一个数与{2,3,5,7}中其中一个的乘积,于是答案就出来了~~

#include<iostream>
using namespace std;
long long humble[5843];
int prime[4]={2,3,5,7};
int N;
int main()
{
    int i,j,k;
    humble[0]=1;
    humble[1]=1;
    //humble[1]=1;
    for(i=2;i<=5842;i++)
    {
        humble[i]=3000000000;
        for(j=0;j<4;j++)
        {
            for(k=i-1;k>=1;k--)
            {
                if(humble[k]*prime[j]<=humble[i-1]){break;}
                if(humble[k]*prime[j]<humble[i])
                {
                    humble[i]=humble[k]*prime[j];
                }
            }
        }
    }
    while(cin>>N)
    {
        if(N==0){break;}
        if(N%10==1&&N%100!=11)
        {
            cout<<"The "<<N<<"st humble number is "<<humble[N]<<"."<<endl;
            continue;
        }
        if(N%10==2&&N%100!=12)
        {
            cout<<"The "<<N<<"nd humble number is "<<humble[N]<<"."<<endl;
            continue;
        }
        if(N%10==3&&N%100!=13)
        {
            cout<<"The "<<N<<"rd humble number is "<<humble[N]<<"."<<endl;
            continue;
        }
        cout<<"The "<<N<<"th humble number is "<<humble[N]<<"."<<endl;
    }
    return 0;
}


posted on 2011-05-22 22:14  lonelycatcher  阅读(731)  评论(0编辑  收藏  举报

导航