Humble Numbers HDU - 1058

Posted 啦啦啦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Humble Numbers HDU - 1058相关的知识,希望对你有一定的参考价值。

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",n,map[n]);
39         else if(n%10==2&&n%100!=12) printf("The %dnd humble number is %d.\n",n,map[n]);
40         else if(n%10==3&&n%100!=13) printf("The %drd humble number is %d.\n",n,map[n]);
41         else printf("The %dth humble number is %d.\n",n,map[n]);
42     }
    return 0;
43 }

 

以上是关于Humble Numbers HDU - 1058的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1058 Humble Numbers

HDU1058 Humble Numbers(DP)

(HDU)1058 --Humble Numbers( 丑数)

hdu1058Humble Numbers(动态规划)

hdu 1058 Humble Numbers

HDU 1058 Humble Numbers