HDU1058Humble Numbers

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1058

水水更健康。。

代码:
#include <cstdio>
#include <queue>
#include <set>

using namespace std;
typedef long long ll;
ll ans[6000];
ll prime[] = {2,3,5,7};

void init()

{
    priority_queue<ll,vector<ll>,greater<ll> >pq;
    set<ll> st;
    int cnt = 1;
    pq.push(1);
    st.insert(1);
    while(true)
    {
        if(cnt >= 5843)
            break;
        ll to = pq.top();
        pq.pop();
        ans[cnt++] = to;
        for(int i = 0;i < 4;++i)
        {
            ll t = to * prime[i];
            if(!st.count(t)){
                st.insert(t);
                pq.push(t);
            }
        }
    }
}
int main()

{
    init();
    int n;
    while(~scanf("%d",&n) && n)
    {
        if(n % 10 == 1 && n % 100 != 11)
            printf("The %dst humble number is %lld.\n",n,ans[n]);
        else if(n % 10 == 2 && n % 100 != 12)
            printf("The %dnd humble number is %lld.\n",n,ans[n]);
        else if(n % 10 == 3 && n % 100 != 13)
            printf("The %drd humble number is %lld.\n",n,ans[n]);
        else
            printf("The %dth humble number is %lld.\n",n,ans[n]);
    }
    return 0;
}

你可能感兴趣的:(set,ACM,优先队列,水)