您的位置:首页 > 其它

hdu 1058 humble number

2015-09-07 23:02 316 查看
题意:当一个数只有2、3、5、7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少;
我自己的生成数组的方法是:数组的第一个元素定为1,然后用优先队列,首先将2,3,5,7放入优先队列,每次从队列中取出最小的一个数,将它放入数组中,然后分别将它乘上2,3,5,7后放入优先队列中,这样重复直到从队列中取出足够的数的时候就结束。对于处理重复元素,只要将优先队列中拿出的元素与数组中的上一次放入的数比较,如果相等就不进行操作,若不等则进行操作,这样就可以了

//  Created by Chenhongwei in 2015.
//  Copyright (c) 2015 Chenhongwei. All rights reserved.

#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
ll a[6000];
ll p[]={2,3,5,7};
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
priority_queue<ll,vector<ll>,greater<ll> >q;
q.push(1);
a[0]=0;
for(int i=1;i<=5842;i++)
{
while(q.top()==a[i-1])
q.pop();
a[i]=q.top();
// cout<<a[i]<<endl;
q.pop();
for(int j=0;j<=3;j++)
q.push(a[i]*p[j]);
}
int n;
while(cin>>n&&n)
{
if(n%10==1&&n%100!=11)
printf("The %dst humble number is %lld.\n",n,a
);
else if(n%10==2&&n%100!=12)
printf("The %dnd humble number is %lld.\n",n,a
);
else if(n%10==3&&n%100!=13)
printf("The %drd humble number is %lld.\n",n,a
);
else
printf("The %dth humble number is %lld.\n",n,a
);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: