• Humble Numbers HDU


    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
    InputThe 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.
    OutputFor 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.

    这道题的输出简直有毒。。。
    题解:假设x是丑数那么2x,3x,5x,7x也是丑数,所以每次取已生成的丑数中的最小值,生成新的丑数,注意避免重复,然后再压入优先队列!
     1 #include<queue>
     2 #include<set>
     3 #include<queue>
     4 #include<cstdio> 
     5 #include<string>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 typedef long long ll;
    11 
    12 ll map[6000];
    13 int num[4]={2,3,5,7};
    14 
    15 void init()
    16 {   set<ll> s;
    17     priority_queue<ll,vector<ll>,greater<ll> > q;
    18     q.push(1);
    19     for(int i=1;i<=5842;i++){
    20         ll temp=q.top();q.pop();
    21         map[i]=temp;
    22         for(int j=0;j<4;j++){
    23             ll x=num[j]*temp;
    24             if(s.count(x)==0){
    25                 q.push(x); 
    26                 s.insert(x);
    27             }
    28         }
    29     }
    30     return ;
    31 }
    32 
    33 int main()
    34 {   init();
    35     
    36     int n;
    37     while(cin>>n&&n){
    38         if(n%10==1&&n%100!=11) printf("The %dst humble number is %d.
    ",n,map[n]);
    39         else if(n%10==2&&n%100!=12) printf("The %dnd humble number is %d.
    ",n,map[n]);
    40         else if(n%10==3&&n%100!=13) printf("The %drd humble number is %d.
    ",n,map[n]);
    41         else printf("The %dth humble number is %d.
    ",n,map[n]);
    42     }
        return 0;
    43 }
  • 相关阅读:
    hdu 3335 Divisibility
    最小点覆盖,最小路径覆盖
    hdu 4109 Instrction Arrangement
    sjtu 1077 加分二叉树
    hdu 1542 Atlantis
    多线程中互斥体
    在子页面中,javascript让模板页中添加的用户控件中的控件选中focus
    模板页中用javascript判断是否为空
    控件包含代码块(即 <% ... %>),因此无法修改控件集合 asp.net
    lambda从指定集合中去除指定数据 asp.net
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7277311.html
Copyright © 2020-2023  润新知