HDU 1058

我用优先级队列做的,需要打表不然会超时。

注意:1200也是rd所以判断条件应该为n%100!=…

/*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.*/
#include<iostream>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
int a[]= {2,3,5,7};
    ll h[6000];
int main()
{


    int n;

    set<ll> s;
    priority_queue<ll,vector<ll>,greater<ll> > pq;
    pq.push(1);
    s.insert(1);
    ll t;
    for(int i=1;i<=5842; i++)
    {
        t=pq.top();
        h[i]=t;
        pq.pop();
        for(int j=0; j<4; j++)
        {
            if(!s.count(t*a[j]))
            {
                s.insert(t*a[j]);
                pq.push(t*a[j]);
            }
        }
    }
    while(cin>>n&&n)
    {
        if(n%10==2&&n%100!=12)
        {
            cout<<"The "<<n<<"nd"<<" humble number is "<<h[n]<<"."<<endl;
        }
        else if(n%10==1&&n%100!=11)
        {
            cout<<"The "<<n<<"st"<<" humble number is "<<h[n]<<"."<<endl;
        }
        else if(n%10==3&&n%100!=13)
        {
            cout<<"The "<<n<<"rd"<<" humble number is "<<h[n]<<"."<<endl;
        }
        else
        {
            cout<<"The "<<n<<"th"<<" humble number is "<<h[n]<<"."<<endl;
        }
    }
return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页