zoukankan      html  css  js  c++  java
  • humble number(hd1058)

    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.
     1 /*看题解AC的,结果开始开编译错误了几次,oj说是min()没定义,那为什么自己编译会通过,然后自己写了min(),dp方程 dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))*/
     2 #include<stdio.h>/*dev编译后运行程序中途闪退,提交后却对的*/
     3 using namespace std;
     4 int min(int a,int b)
     5 {
     6     return a>b?b:a;
     7 }
     8 int humble[5843];
     9 void factor()/*马丹看了半天也是似懂非懂,比赛时完全想不出来这么求*/
    10 {
    11     int post1=1,post2=1,post3=1,post4=1,na,nb,nc,nd,i;
    12     humble[1]=1;
    13     for(i=2;i<=5843;i++)
    14     {
    15         humble[i]=min(na=humble[post1]*2,min(nb=humble[post2]*3,min(nc=humble[post3]*5,nd=humble[post4]*7)));
    16         if(humble[i]==na) post1++;
    17         if(humble[i]==nb) post2++;
    18         if(humble[i]==nc) post3++;
    19         if(humble[i]==nd) post4++;
    20     }
    21 }
    22 int main()
    23 {
    24     int n;
    25     factor();
    26     while(~scanf("%d",&n))
    27     {
    28         if(n==0)
    29             break;
    30         printf("The %d",n);
    31         if(n%100!=11&&n%10==1)
    32             printf("st");
    33         else if(n%100!=12&&n%10==2)
    34             printf("nd");
    35         else if(n%100!=13&&n%10==3)
    36             printf("rd");
    37         else
    38             printf("th");
    39         printf(" humble number is %d.
    ",humble[n]);
    40     }    
    41 }
  • 相关阅读:
    HDOJ1269 迷宫城堡
    最长公共子序列 nyoj36
    HDU1081 To The Max 求子矩阵最大和
    nyoj20 吝啬的国度
    景观分析工具:arcgis中patch analysis模块
    景观格局动态变化分析方法(基于ArcGIS、Fragstats、ENVI、ERDAS、Patch Analysis for ArcGIS) (20110315 08:07:03)
    从C#到Python —— 谈谈我学习Python一周来的体会
    如何判定多边形是顺时针还是逆时针
    超新星与暗能量的发现--今年诺贝尔物理奖工作的介绍(转)
    怎样把扫描好的身份证打印出实际大小
  • 原文地址:https://www.cnblogs.com/a1225234/p/4498976.html
Copyright © 2011-2022 走看看