hdu 1058 humble number
2015-09-07 23:02
316 查看
题意:当一个数只有2、3、5、7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少;
我自己的生成数组的方法是:数组的第一个元素定为1,然后用优先队列,首先将2,3,5,7放入优先队列,每次从队列中取出最小的一个数,将它放入数组中,然后分别将它乘上2,3,5,7后放入优先队列中,这样重复直到从队列中取出足够的数的时候就结束。对于处理重复元素,只要将优先队列中拿出的元素与数组中的上一次放入的数比较,如果相等就不进行操作,若不等则进行操作,这样就可以了
我自己的生成数组的方法是:数组的第一个元素定为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; }
相关文章推荐
- Ubuntu下 Redis安装
- UVA 11992 Fast Matrix Operations (降维)
- JQ之html,text,val
- objective-c ios webkit 本地存储local-storage
- Oracle同义词synonym用法小结
- Cocos2dx引擎笔记——综述
- Hibernate(二)
- openCobol 安装 windows下cobol执行环境做成
- JS设计模式学习实例之单例模式
- Remove Nth Node From End of List
- 性能测试过程
- C#使用Func
- uva 1146 - Now or later(二分+2sat)
- Android APK反编译就这么简单 详解(附图)
- 图片资源过多 视频资源过多 导致的编译 速度 过慢问题
- 数据库中的约束
- 微信公共平台开发3 .net
- react-native试玩(12)-iOS中导航栏
- Dialog大小控制和Dialog中使用的ListView自适应
- 第94讲:Akka第一个案例动手实战MapActor、ReduceActor、AggregateActor代码详解学习笔记