hdu 1058 Humble Numbers

因为数字达到了2亿。所以用不了筛选。我用的是bfs+map判重

至于输出格式.......看代码吧~

#include<iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<algorithm>
#include<cstring>
#define maxn 2000000000+5
#define ll long long
using namespace std;
int f[6000];
priority_queue<ll, vector<ll>, greater<ll> >mapp;
map<ll,int>root;
void solve()
{
	ll x=1;
	mapp.push(x);
	int k=0;
	while(mapp.size())
	{
		x=mapp.top();
		mapp.pop();
		f[k++]=x;
		if(x*2<maxn&&root.find(x*2)==root.end()) mapp.push(2*x),root[2*x]=1;
		if(x*3<maxn&&root.find(x*3)==root.end()) mapp.push(3*x),root[3*x]=1;
		if(x*5<maxn&&root.find(x*5)==root.end()) mapp.push(5*x),root[5*x]=1;
		if(x*7<maxn&&root.find(x*7)==root.end()) mapp.push(7*x),root[7*x]=1;
	} 
}
int main()
{
	solve();
	int m;
	while(scanf("%d",&m)!=EOF&&m)
	{
		if(m%10==1 && m%100!=11) printf("The %dst humble number is %d.\n",m,f[m-1]);
        else if(m%10==2 && m%100!=12) printf("The %dnd humble number is %d.\n",m,f[m-1]);
        else if(m%10==3 && m%100!=13) printf("The %drd humble number is %d.\n",m,f[m-1]);
        else printf("The %dth humble number is %d.\n",m,f[m-1]);
	}
	return 0;
}


 

posted on 2017-05-21 18:32  blfbuaa  阅读(83)  评论(0编辑  收藏  举报