HDU1058Humble Numbers

来源:互联网 发布:java报表管理系统源码 编辑:程序博客网 时间:2024/06/07 06:27
题目链接: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;}

0 0
原创粉丝点击