hdu---1058Humble Numbers

来源:互联网 发布:专业淘宝拍摄 编辑:程序博客网 时间:2024/05/01 11:34

Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14705    Accepted Submission(s): 6375


Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble 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 humble numbers. 

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

 

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

 

Output
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.
 

 

Sample Input
1234111213212223100100058420
 

 

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.
 
/*    思路:    超时方法:    可以看出: the humble number的因子中不含大于等于11的素因子。    先打表求出前面的一些素数,之后筛选出其所有的倍数即可。 余下的都为 humble numbers。    AC方法: 因为因子一共只有4个, 一个humble number可以写成2^a*3^b*5^c*7^d的形式;        然后枚举abcd,计算出每一个humble number,再排序即可!*///错误代码:/*#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#define N 5001000int f[N];int ff[N];int main(){    int i,j,k=0;int flag=0;    freopen("in.txt","r",stdin);    int n,m;    memset(f,0,sizeof(f));    for(i=2;i<1000000;i++)        for(j=i+i;j<N;j=j+i)            f[j]=1;  //为1代表不是素数;    for(i=11;i<N;i++)        if(f[i]==0) //是素数;            for(j=i;j<N;j=j+i)                ff[j]=1; //为1标明不是 humble number;    int num=0;    for(i=1;i<N;i++){        if(ff[i]==0){            f[num++]=i;        }        if(num>6000) break;    }    while(scanf("%d",&n)){        if(n==0) break;        printf("%d\n",f[n-1]);    }    printf("%d\n",num);    return 0;}*//*      错误代码2:    枚举错误!  a不止取到9  类似b c d 也一样。  使得数组中少了一部分,结果WA。    需要换一种枚举方式!*//*#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#define M 2000000000#define N 10000long long f[N];int main(){    long long i,j,t=0,tt,k=0;    freopen("in.txt","r",stdin);    long long n,m;long long num=0;    long long a,b,c,d;    t=0;    while(t<10000){        num=1;tt=t;        d=tt%10;tt=tt/10;        c=tt%10;tt=tt/10;        b=tt%10;tt=tt/10;        a=tt%10;        int flag=1;        while(a--) num=num*2;        while(b--){            if(num>2000000000){                flag=0;break;            }            num=num*3;            if(num>2000000000){                            flag=0;break;                        }        }        while(c--){            if(num>2000000000){                flag=0;break;            }            num=num*5;            if(num>2000000000){                            flag=0;break;                        }        }        while(d--){            if(num>2000000000){                flag=0;break;            }            num=num*7;            if(num>2000000000){                flag=0;break;            }        }        if(flag){            f[k++]=num;        }        t++;    }    for(i=0;i<k;i++)        for(j=0;j<k-i-1;j++)            if(f[j]>f[j+1]){                long long kk=f[j];f[j]=f[j+1];f[j+1]=kk;            }    for(i=0;i<N;i++)        if(f[i]!=0) break;    j=i-1;    while(scanf("%lld",&k)){        if(k==0) break;        printf("%lld\n",f[k+j]);    }    return 0;}*///AC代码: (转自fudq,学习)#include<iostream>using namespace std;const int M=5842;int h[M];int Min(int a,int b,int c,int d){    a=(a<b)?a:b;    c=(c<d)?c:d;    return (a<c)?a:c;}void deal(){    int i,in1=0,in2=0,in3=0,in4=0,M1,M2,M3,M4;    h[0]=1;    for(i=1;i<M;i++) //注意打表的方法:这里是按照从小到大的顺序直接打出了表,不需要再排序。    {        M1=2*h[in1];        M2=3*h[in2];        M3=5*h[in3];        M4=7*h[in4];        h[i]=Min(M1,M2,M3,M4);        if(h[i]==M1)            in1++;        if(h[i]==M2)            in2++;        if(h[i]==M3)            in3++;        if(h[i]==M4)            in4++;    }}int main(){    freopen("in.txt","r",stdin);    deal();    char t[3];    int n;    while(scanf("%d",&n) && n)    {        if(n%100!=11 && n%10==1) //注意n%100!=11,n%100!=12,n%100!=13的情况            strcpy(t,"st");        else if(n%100!=12 && n%10==2)            strcpy(t,"nd");        else if(n%100!=13 && n%10==3)            strcpy(t,"rd");        else            strcpy(t,"th");        printf("The %d%s humble number is %d.\n",n,t,h[n-1]);    }    return 0;}

 

 
0 0