hdu---1058Humble Numbers

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
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.
 
/*
    思路:
    超时方法:
    可以看出: 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 5001000
int 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 10000
long 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的简单俄罗斯方块游戏(附源代码) 项目:使用 JavaScript 编写的简单俄罗斯方块游戏(附源代码) 该游戏是一个使用 HTML5 和 JavaScript 开发的简单项目。这款游戏允许玩家策略性地旋转下落的方块以清除关卡。要在此游戏中得分,您必须通过操纵方块来清除线条,使它们填满水平行。当方块掉落时,您必须 根据需要移动 和旋转它们,使它们均匀地排列在底部。 游戏制作 该游戏仅使用 HTML、CSS 和 JavaScript。谈到这款游戏的功能,这款游戏的 PC 控制也很简单。首先,您必须按空格键才能开始游戏。您可以使用箭头键来更改下落方块的位置。您可以在侧栏看到形成的分数和行。 该游戏包含大量的 javascript,用于对游戏的某些部分进行验证。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,单击 index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值