Humble Numbers(丑数)

版权声明:转载请注明出处链接 https://blog.csdn.net/qq_43408238/article/details/88909019

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.

作为英语弱鸡的我真是对这题目瞪口呆,一开始看到这题,哇哦,这个题目不是和那个丑数题目差不多吗,看我三下五除二做出来,然后做的时候忘了优先队列的用法(脸黑),又回顾了一下优先队列的用法,这下总算做出来了吧,一直WAWAWA ,WA到我快崩溃了,算了,借鉴一下别人的博客怎么写的吧,额,原来是这样。

还用到英语。。。orz。
一般来说个位数是1为st,个位数为2是nd,个位数为3是rd;但是有例外,11, 12 ,13及以他们为结尾的数字均为th,如111,112,113也为th,除此之外,其他都是th;

附上我的代码和大神的DP代码:

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include <algorithm>
#include <stdlib.h>
#include <cstdio>
#include<sstream>
#include<cctype>
#include <set>
#include<queue>
#include <map>
#include <iomanip>
typedef long long ll;
using namespace std;
ll ans[]={0,2,3,5,7};
int main()
{
    //freopen("input.txt","r",stdin);
    priority_queue<ll ,vector<ll>,greater<ll> > v;v.push(1);
    vector<ll> sd;sd.push_back(1);
    set<ll> st;st.insert(1);
    ll t;ll sum=1;
    while(1)
    {
        if(sum>=6000) break;
        ll k=v.top();v.pop();sum++;
        for(ll i=1;i<=4;i++)
        {
            ll l=ans[i]*k;
            if(st.count(l)==0) {v.push(l);sd.push_back(l);st.insert(l);}
        }
    }


    sort(sd.begin(),sd.end());



    while(scanf("%lld",&t)!=EOF)
    {
            if(t==0) break;
            if(t%10==1&&t%100!=11) printf("The %lldst humble number is %lld.\n",t,sd[t-1]);
            else if(t%10==2&&t%100!=12)   printf("The %lldnd humble number is %lld.\n",t,sd[t-1]);
            else if(t%10==3&&t%100!=13)   printf("The %lldrd humble number is %lld.\n",t,sd[t-1]);
            else   printf("The %lldth humble number is %lld.\n",t,sd[t-1]);


    }
}

DP代码(隐含迭代思想):

#include <iostream>
#include <algorithm>
using namespace std;
#define M 50000
__int64 min(__int64 a,__int64 b,__int64 c,__int64 d){  
    __int64 x=a<b?a:b;  
    __int64 y=c<d?c:d;  
    return x<y?x:y;  
}  
__int64 vis[M];
int main(__int64 p2,__int64 p3,__int64 p5,__int64 p7)
{   
    int i=1,n,cur;
    p2=p3=p5=p7=1;
    memset(vis,0,sizeof(vis)); vis[1]=1;
    while(vis[i]<=2000000000)
    {
        vis[++i]=min(vis[p2]*2,vis[p3]*3,vis[p5]*5,vis[p7]*7);
        if(vis[i]==vis[p2]*2) p2++;  
        if(vis[i]==vis[p3]*3) p3++;  
        if(vis[i]==vis[p5]*5) p5++;  
        if(vis[i]==vis[p7]*7) p7++;  
    }
    while(scanf("%d",&n)!=EOF,n)
    {
        string ss;  
        if(n%10==1&&n%100!=11)  
            printf("The %dst humble number is %I64d.\n",n,vis[n]);  
        else if(n%10==2&&n%100!=12)  
            printf("The %dnd humble number is %I64d.\n",n,vis[n]);  
        else if(n%10==3&&n%100!=13)  
            printf("The %drd humble number is %I64d.\n",n,vis[n]);  
        else   
            printf("The %dth humble number is %I64d.\n",n,vis[n]);  
        
    }  
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43408238/article/details/88909019