http://acm.hdu.edu.cn/showproblem.php?pid=1058
输出部分还是背下来吧 英语太渣= =
使用优先队列维护当前找到的数字,使用集合容器来判断数字是否重复。
#include <set>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Max = 5842;
LL ans[Max+10];
int n,cnt;
int a[4] = {2,3,5,7};
void make_humble_number(){
priority_queue <LL, vector<LL>, greater<LL> > q;
set <LL> myset;
myset.clear();
q.push(1),myset.insert(1);
while(!q.empty()){
LL before = q.top();
q.pop();
ans[cnt++] = before;
if(cnt >= Max) break;
for(int i=0; i<4; i++){
LL now = before * a[i];
if(!myset.count(now)){///如果集合里没有这个数字那么插入(无法用数组来记录会超内存)
myset.insert(now);
q.push(now);
}
}
}
return;
}
int main(){
// freopen("in.txt", "r", stdin);
make_humble_number();///离线处理
while(scanf("%d",&n) != EOF && 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 %I64d.\n", ans[n-1]);
}
return 0;
}