hdu1058

题意:当一个数只有2、3、5、7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少;

其实并没有发现hdu把这道题放在 dp 专题里的意图,我的思路就是预处理出丑数数组,然后读入 n 就直接输出第 n 个丑数。我自己又一种想法,询问翔神之后又学到了另一种做法。

我自己的生成数组的方法是:数组的第一个元素定为1,然后用优先队列,首先将2,3,5,7放入优先队列,每次从队列中取出最小的一个数,将它放入数组中,然后分别将它乘上2,3,5,7后放入优先队列中,这样重复直到从队列中取出的数大于2000000000的时候就结束。对于处理重复元素,只要将优先队列中拿出的元素与数组中的上一次放入的数比较,如果相等就不进行操作,若不等则进行操作,这样就可以了。

 #include<stdio.h>
#include<queue>
using namespace std;
#define LL long long
LL hum[],count=; void init(){
// printf("1111111111");
priority_queue<LL ,vector<LL>,greater<LL> >q;
q.push();
q.push();
q.push();
q.push();
hum[]=;
LL a=q.top();
q.pop(); while(a<=){
if(a!=hum[count]){
hum[++count]=a;
q.push(a*);
q.push(a*);
q.push(a*);
q.push(a*);
}
a=q.top();
q.pop();
}
return;
} int main(){
// printf("1111111111");
init();
int n;
while(scanf("%d",&n)!=EOF&&n!=){
printf("The %d",n);
if(n%==||n%==||n%==)printf("th ");
else if(n%==)printf("st ");
else if(n%==)printf("nd ");
else if(n%==)printf("rd ");
else printf("th ");
printf("humble number is %lld.\n",hum[n]);
}
return ;
}

翔神告诉我另一种做法,首先数组 hum[10000] 第一个元素还是1,然后定 a2 , a3 , a5 , a7 四个数分别表示 2 3 5 7 接下来要乘的数组元素的下标,起始都为 1 ,分别比较 2 * hum [ a2 ] , 3* hum [ a3 ] ,5 * hum [ a5 ] ,7 * hum [ a7 ] ,最小的一个放入数组,并将其对应的数组下标 a几 ++,重复直到超过2000000000

 #include<stdio.h>
#define LL long long
LL hum[]; void init(){
hum[]=;
LL a2=,a3=,a5=,a7=,count=;
while(hum[count]<){
LL min=;
if(*hum[a2]<min)min=*hum[a2];
if(*hum[a3]<min)min=*hum[a3];
if(*hum[a5]<min)min=*hum[a5];
if(*hum[a7]<min)min=*hum[a7];
hum[++count]=min;
if(*hum[a2]==min)a2++;
if(*hum[a3]==min)a3++;
if(*hum[a5]==min)a5++;
if(*hum[a7]==min)a7++;
}
return;
} int main(){
init();
int n;
while(scanf("%d",&n)!=EOF&&n!=0i){
printf("The %d",n);
if(n%==||n%==||n%==)printf("th ");
else if(n%==)printf("st ");
else if(n%==)printf("nd ");
else if(n%==)printf("rd ");
else printf("th ");
printf("humble number is %lld.\n",hum[n]);
}
return ;
}

hdu1058丑数(优先队列、暴力打表)的更多相关文章

  1. 264.丑数II

    题目 给你一个整数 n ,请你找出并返回第 n 个 丑数 . 丑数 就是只包含质因数 2.3 和/或 5 的正整数. 示例 1: 输入:n = 10 输出:12 解释:[1, 2, 3, 4, 5, ...

  2. UVA - 136 Ugly Numbers(丑数,STL优先队列+set)

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...

  3. 洛谷P2723 丑数 Humble Numbers

    P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...

  4. LeetCode——264. 丑数 II

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  5. BSOJ 1562 【堆练习】丑数3576

    Description 丑数是指素因子都在集合{2,3,5,7}内的整数,第一个丑数是1. 现在输入n(n<=4000),输出第n个丑数. Input 输入文件仅一行为一个整数n. Output ...

  6. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  7. 【python】Leetcode每日一题-丑数2

    [python]Leetcode每日一题-丑数2 [题目描述] 给你一个整数 n ,请你找出并返回第 n 个 丑数 . 丑数 就是只包含质因数 2.3 和/或 5 的正整数. 示例1: 输入:n = ...

  8. hdu 1431 素数回文(暴力打表,埃托色尼筛法)

    这题开始想时,感觉给的范围5 <= a < b <= 100,000,000太大,开数组肯定爆内存,而且100000000也不敢循环,不超时你打我,反正我是不敢循环. 这题肯定得打表 ...

  9. UVA136 求第1500个丑数

    枚举大范围数据..暴力检查题目条件 #include <iostream> #include <cstdio> #include <vector> #include ...

随机推荐

  1. 打饭助手之NABC

    Need: 同学们在早上跑操后要吃早饭,还有中午打饭时人更是多.常常要排很长的队伍,造成时间的浪费,和焦急的等待.因此我们需要错开打饭的高峰期,来避免打饭排队的悲哀. Approach: 通过获取摄像 ...

  2. 转:gartner 2014-07 ETL工具象限排名

    ref:  http://www.gartner.com/technology/reprints.do?id=1-1YAXV15&ct=140728&st=sb

  3. iOS NSDictionary、NSData、JSON数据类型相互转换

    iOS经常需要用到数据类型的转换,下面列举一下常用类型的转换. 1.NSDictionary类型转换为NSData类型: //NSDictionary -> NSData: NSDictiona ...

  4. (转)phoneGap-Android开发环境搭建

    (原)http://www.cnblogs.com/shawn-xie/archive/2012/08/15/2638480.html phoneGap-Android开发环境搭建   一.安装 在安 ...

  5. 认真对待每一道算法题 之 两个排序好的数组寻找的第k个大的数

    转载博客:http://www.cnblogs.com/buptLizer/archive/2012/03/31/2427579.html 题目意思:给出两个排好序的数组 ,不妨设为a,b都按升序排列 ...

  6. C# 调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配

    在dllimport中加入CallingConvention参数就行了,[DllImport(PCAP_DLL, CharSet = CharSet.Auto, CallingConvention = ...

  7. ubuntu文件夹右键没有共享选项

    在配置samba的时候,不知道出了什么错误,我就删除了samba,之后在ubuntu文件上按右键就没有共享的选项了,这样每次配置都得进samba麻烦. 我重新安装了samba也不行,郁闷! 解决: 1 ...

  8. Python _ 开始介绍对象

    Python的私有变量,函数是在前面加上一个双下划线'__'来声明的,气访问和C++大同小异 例如 class Person: __name='小甲鱼' def print1(self): # 和 c ...

  9. dll强签名的由来和作用

    C# dll强签名介绍 之前基本没有这个概念,直到有一天我们的dll被反编译了,导致我们的代码基本上被看到了,才想起来要保护dll的安全性,因为C#语言的在编译过程中会产生中间语言导致dll很容易被反 ...

  10. HDU 1005 Number Sequence(AC代码)

    #include <stdio.h> #include <string.h> int main() { int a,b,n; int i; ]={}; f[]=; f[]=; ...