HDU 1058 Humble Numbers

题解:用优先队列,建立小根堆,然后每次取出最小的,将其乘上题目要求的数字再加入优先队列,至于重复的问题,用映射来解决

HDU 1058 Humble Numbers
#include <queue>  
#include <map>  
#include <iostream>  
#include <functional>  
using namespace std;  
#define MAXI 5843  
  
priority_queue<long long, vector<long long>, greater<long long> > Q;   
long long hn[MAXI], l;  
map<long long, bool> exi;  
  
void init()  
{  
    long long tmp;  
    l = 1;  
    Q.push(1);  
    while (l<MAXI)  
    {  
        tmp = Q.top();  
        Q.pop();  
        if (exi[tmp]) continue;  
        exi[tmp]=1;  
        hn[l++]=tmp;  
        Q.push(tmp*2);  
        Q.push(tmp*3);  
        Q.push(tmp*5);  
        Q.push(tmp*7);  
    }  
}  
  
int main()  
{  
    int n;  
  
    init();  
    while (scanf("%d",&n),n)   
    {  
        printf("The %d", n);  
        if (n%10==1&&n%100!=11) printf("st ");  
        else if (n%10==2&&n%100!=12) printf("nd ");  
        else if (n%10==3&&n%100!=13) printf("rd ");  
        else printf("th ");  
        printf("humble number is ");
        cout<<hn[n]<<"."<<endl;  
    }  
  
    return 0;  
}  
HDU 1058 Humble Numbers

HDU 1058 Humble Numbers

上一篇:连接池


下一篇:iOS 开发者必不可少的 75 个工具