首页 > 代码库 > hdu 1058 Humble Numbers || poj 1338(dp)

hdu 1058 Humble Numbers || poj 1338(dp)

 

两题都是一样的题目 只是hdu 1058 多了个7

 

题意:求一个每个数因子仅含2 3 5 7 的 序列

         问 第n个数是几

 

思路:

        ans[i]=min(min(ans[n2]*2,ans[n3]*3),min(ans[n5]*5,ans[n7]*7));          if(ans[i]==ans[n2]*2) n2++;        if(ans[i]==ans[n3]*3) n3++;        if(ans[i]==ans[n5]*5) n5++;        if(ans[i]==ans[n7]*7) n7++;

hdu 1058

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int INF=2000000005;int ans[6000];int main(){    int coun=2;    ans[1]=1;    int n,i,j,k;    int n2=1,n3=1,n5=1,n7=1;    for(i=2;i<=5842;i++)    {        ans[i]=min(min(ans[n2]*2,ans[n3]*3),min(ans[n5]*5,ans[n7]*7));        if(ans[i]==ans[n2]*2) n2++;        if(ans[i]==ans[n3]*3) n3++;        if(ans[i]==ans[n5]*5) n5++;        if(ans[i]==ans[n7]*7) n7++;    }    while(scanf("%d",&n),n)    {        if(n%10==1&&n%100!=11)        printf("The %dst humble number is %d.\n",n,ans[n]);        else if(n%10==2&&n%100!=12)        printf("The %dnd humble number is %d.\n",n,ans[n]);        else if(n%10==3&&n%100!=13)        printf("The %drd humble number is %d.\n",n,ans[n]);        else        printf("The %dth humble number is %d.\n",n,ans[n]);    }    return 0;}

 

hdu 1058 Humble Numbers || poj 1338(dp)