动态规划--Humble Numbers

Humble Numbers

Many of you heard about ugly number. Now we are introducing a new number that is called bubble number.
A number whose only prime factors are 2,3,5 or 7 is called a bubble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 bubble numbers.

Write a program to find and print the nth element in this sequence.

Input Specification
The input consists of one or more test cases. Each test case consists of one integer n with . Input is terminated by a value of zero (0) for n.

Output Specification

For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output. (here humble means bubble)

See http://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers for more explanation of using correct suffix

Sample Input

1
2
3
4
11
12
13
21
22
23
100
1000
5842
0


Sample Output



The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.

自己做超时了...参考了别人的代码.....

题意:找出第n个bubble number, bubble numbers就是那些因子是2或3或5或7的数.2000000000太大,用数组来标记编译都不能通过,

 11用英语是the eleventh  12是the twelfth 13是the thirteenth 1 first 2 second 3 third

第n个bubble number肯定是(前n-1个bubble number中*2或*3或*5或*6)中最小的一个


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

int main()
{
    int x[4],a[6000]= {1},i,min,m,h,k,l,n;
    a[1]=1;
    l=m=k=h=2;
    for(i=2; i<=5842; i++)
    {
        x[0]=a[m-1]*2;                //当m=3,则表示a[1]已经乘过2了,要将a[2]*2的结果和a[1]*3,a[1]*5,a[1]*7比较,选出最小的a[1]*3作为第三个bubble number,然后将h++,说明a[1]已经乘过3了,要将a[2]*3与a[2]*2,a[1]*5,a[1]*7相比较,选出最小的,以此类推
        x[1]=a[h-1]*3;
        x[2]=a[k-1]*5;
        x[3]=a[l-1]*7;
        min=x[0];
        if(x[1]<min)
            min=x[1];
        if(x[2]<min)
            min=x[2];
        if(x[3]<min)
            min=x[3];
        if(min==x[0])
            m++;
        if(min==x[1])
            h++;
        if(min==x[2])
            k++;
        if(min==x[3])
            l++;
        a[i]=min;
    }
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        if(n%10==1&&n%100!=11)
            printf("The %dst humble number is %d.\n",n,a[n]);
        else if(n%10==2&&n%100!=12)
            printf("The %dnd humble number is %d.\n",n,a[n]);
        else if(n%10==3&&n%100!=13)
            printf("The %drd humble number is %d.\n",n,a[n]);
        else
            printf("The %dth humble number is %d.\n",n,a[n]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值