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. jQuery 对dom的操作

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. VS2013开发Android App 环境搭建

    下载并安装vs2013,(安装时发现多了with blend,百度后有人说是设计师用版本,这是不对的,害我花费不少时间查找程序员用版本).我安装的是Microsoft Visual Studio Ul ...

  3. linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程

    1.设置任务调度命令crontab 任务调度是指系统在某个时间执行的特定的命令或程序.任务调度分为:1)系统工作:有些重要的工作必须周而复始的执行,如病毒扫描.2)个别用户工作:个别用户可能希望执行某 ...

  4. MySQL语句45道练习题及答案

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  5. 网络数据的XML解析

    网络应用中的数据解析,因为最近的应用,无论是Android的和ios平台的,一直用也是建议用的都是Json解析, xml解析都有点被遗忘了. 然后最近自己在做着玩一个ios的小应用,涉及网络数据的抓取 ...

  6. python3 nonlocal vs global

    考虑这样一个python程序: x = 12 def func(): x = 1 func() print(x) 输出为:x = 12 因为函数内部定义的x被认为只属于局部作用域,为了表明我么引用的是 ...

  7. Helo command rejected: need fully-qualified hostname

    Helo command rejected: need fully-qualified hostname问题 是由于postfix的配置文件(main.cf)有问题.其中有一个smtpd_sasl_l ...

  8. post&get请求总结

    1.将get获取的数据,UrlDecode后返回 public static string SendGet(string url) { HttpWebRequest httpWebRequest = ...

  9. web字体

    <span style="font-family:sans-serif">Lorem Ipsum</span> <span style="f ...

  10. HDU 5092

    http://acm.hdu.edu.cn/showproblem.php?pid=5092 卡读题,实质是每行取一个点,从上到下找一条路径权值和最小,点可以到达的地方是周围八个格子 类似数塔的dp, ...