DP————1004

题目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[n]);
        else if(n%10==2&&n%100!=12)
  scanf("The %dnd humble number is %d.\n",n,hum[n]); 
        else if(n%10==3&&n%100!=13)
  scanf("The %drd humble number is %d.\n",n,hum[n]);
        else
  scanf("The %dth humble number is %d.\n",n,hum[n]);
    }
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值