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. 网易云课堂 OCP

    数据库DBA任务: 管理数据库可用性 设计并创建数据库 管理物理结构 管理基于设计的存储 管理安全性 网络管理 备份与恢复 数据库调整与优化 关系型数据库(RDBMS) 多个表数据之间存在着关系 关系 ...

  2. springMVC文件上传(转)

    原文链接: http://www.cnblogs.com/lonecloud/p/5989905.html 在Spring-mvc.xml注入bean 1 <!-- 配置文件上传,如果没有使用文 ...

  3. SpringMvc中的反射

    controller中的方法,是通过反射调用的 spring监控controller中的注解,当命令符合某个注解的时候,通过反射,找到这个注解对应的方法,然后调用,处理完成得到返回值,再根据这个返回值 ...

  4. intent 传参数

    一.传递List<String>和List<Integer>以下以传递List<String>为例,发送List<String>语法为:intent.p ...

  5. hdu 2061

    PS:  以为找个简单来恢复信心..结果碰到那么傻逼的题目... 题意:给出学分和成绩,算GPA...关键是注意换行....它要求的换行我觉得超级奇怪...除了第一个正常,其他的输入完之后先一个换行. ...

  6. 极客DIY:打造属于自己的无线移动渗透测试箱

    本文中介绍的工具.技术带有一定的攻击性,请合理合法使用. 你想不想拥有一款属于自己的移动无线渗透测试箱,如果你感兴趣,下面介绍的设备将会对你很有帮助.这个箱子被称为“MiTM(中间人攻击)WiFi箱” ...

  7. BZOJ 1045 糖果传递

    奇怪的式子.最后发现取中位数. #include<iostream> #include<cstdio> #include<cstring> #include< ...

  8. linux命令:cat

    1:命令介绍: cat用来打印标准输入或连接文件.tac是其相反命令,从最后一行开始打印. 2:命令格式: cat [选项] 文件 3:命令参数: -A, --show-all           等 ...

  9. 通过 itms-services 协议,发布或者分享 iOS 应用程序

    导读:itms-services 协议常用于 iOS 企业应用的无线部署,这可在不使用 iTunes 的情况下将内部软件发布或者分享给用户. 一.前期准备资料: 1.应用程序 (.ipa) 文件(使用 ...

  10. 转:Enterprise Library 4.0缓存应用程序块

    英文原文:http://msdn.microsoft.com/zh-cn/library/cc511588(en-us).aspx Enterprise Library 缓存应用程序块允许开发人员在应 ...