zoukankan      html  css  js  c++  java
  • POJ 2247 Humble Numbers

                                                      Humble Numbers
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8704   Accepted: 4115

    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.

    Source

     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 #define MAXN 6000
     6 
     7 long long int a[MAXN];
     8 
     9 using namespace std;
    10 
    11 void print()
    12 {
    13     a[0]=1;
    14     int i1,i2,i3,i4;
    15     int t1,t2,t3,t4;
    16     i1=i2=i3=i4=0;
    17 for(int i=1;i<MAXN;i++)
    18 {
    19     t1=a[i1]*2;
    20     t2=a[i2]*3;
    21     t3=a[i3]*5;
    22     t4=a[i4]*7;
    23 
    24     a[i]=min(min(t1,t2),min(t3,t4));
    25 
    26     if(a[i]==t1)  i1++;
    27     if(a[i]==t2)  i2++;
    28     if(a[i]==t3)  i3++;
    29     if(a[i]==t4)  i4++;
    30 }
    31 
    32 
    33 }
    34 
    35 int main()
    36 {
    37     print();
    38     int n;
    39     while(scanf("%d",&n)!=EOF && n)
    40     {
    41         int res = n % 10;
    42         int rs= n % 100;
    43         if(res==1&&rs!=11)  printf("The %dst humble number is %ld.\n",n,a[n-1]);
    44         else if(res==2&&rs!=12)  printf("The %dnd humble number is %ld.\n",n,a[n-1]);
    45         else if(res==3&&rs!=13)  printf("The %drd humble number is %ld.\n",n,a[n-1]);
    46         else printf("The %dth humble number is %ld.\n",n,a[n-1]);
    47     }
    48 
    49     return 0;
    50 }
  • 相关阅读:
    常用算法解析-动态规划
    转载-通过ApplicationContext 去获取所有的Bean
    什么是crud?
    new 关键字 和 newInstance() 方法的 区别
    Java反射简单使用--第一次细致阅读底层代码
    动态创建管理定时任务-已完成
    springboot mail整合freemark实现动态生成模板
    20190930开始记录每天学习状态,更新至20200125结束
    hibernate的对象/关系映射结果为空,exists查不到值的问题-20190823
    转载-Java中LinkedList的一些方法—addFirst addFirst getFirst geLast removeFirst removeLast
  • 原文地址:https://www.cnblogs.com/CKboss/p/3087555.html
Copyright © 2011-2022 走看看