您的位置:首页 > 其它

DP————1004

2016-05-02 11:09 197 查看
题目humple number

题意:在(0,5842)范围内求仅有2,3,5,7因子的数列,如1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25,              27。。。为前20个humple number

思路:显然,第一个为1,这个数列按从小到大排列,因子只有2,3,5,7四个数,所以第二个数为2,第三个为3,第              四个为2*2,第五个为5,第六个为3*2,第七个为7,而第八个为2*2*2........  数列中关于2的与2的倍数有关,             若此次选择的为2的倍数,下一个关于2的数一定是这次选择的倍数乘2。3,5,7一样

感想:在前20个数中查找规律

代码

#include<iostream>

#include<cmath>

#include<string.h>

#include<stdio.h>

using namespace std;

int main()

{

    long long int hum[5866];

    hum[1]=1;

    int a,b,c,d;

    a=b=c=d=1;

    int i;

    for(i=2;i<5844;i++)

    {

        hum[i]=min(hum[a]*2,min(hum[b]*3,min(hum[c]*5,hum[d]*7)));

        if(hum[i]==hum[a]*2)

            a++;

        if(hum[i]==hum[b]*3)

            b++;

        if(hum[i]==hum[c]*5)

            c++;

        if(hum[i]==hum[d]*7)

            d++;

    }

    int n;

    while(~scanf("%d",&n)!=EOF,n)

    {

        if(n%10==1&&n%100!=11)
scanf("The %dst humble number is %d.\n",n,hum
);

        else if(n%10==2&&n%100!=12)

  scanf("The %dnd humble number is %d.\n",n,hum
); 

        else if(n%10==3&&n%100!=13)

  scanf("The %drd humble number is %d.\n",n,hum
);

        else

  scanf("The %dth humble number is %d.\n",n,hum
);

    }
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: