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. UML学习入门就这一篇文章

    1.1 UML基础知识扫盲 UML这三个字母的全称是Unified Modeling Language,直接翻译就是统一建模语言,简单地说就是一种有特殊用途的语言. 你可能会问:这明明是一种图形,为什 ...

  2. matlab:clear,close,clc

    clear 删除工作空间中的项目,释放系统内存 语法: clear clear name clear name1 name2 name3... clear global name clear -reg ...

  3. [微软]technet与msdn

    我们搜索一个微软术语,有时定位到technet页面,有时定位到msdn页面.我直观的理解就是technet教人们如何使用微软产品,而msdn指导人们如何开发基于微软产品的软件.那么微软对它们具体定位是 ...

  4. ajax 请求超过了5s 还没有返回 的话 就自动取消

    ajax请求时有个参数可以借鉴一下 var ajaxTimeOut = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : ' ...

  5. Canopy使用教程 (3)

    1. 2. plot函数: plot默认生成是曲线图,可以通过kind参数生成其他的图形,可选的值为:line, bar, barh, kde, density, scatter. 散点图.使用kin ...

  6. 程序员最爱 Mac、JS 是最热门技术

    概况: 今年,有超过5万名开发者向我们分享了他们是谁,做什么工作,以及他们的成果.通过本文,你将看到有史以来最为全面的一次开发者情况调查的结果. 每8秒钟,就会有一位开发者在Stack Overflo ...

  7. python字符decode与encode的问题

    同事在工作中遇到一个字符编码的问题:问题是:从mysql数据库中读出来的varchar类型数据在python是unicode类型的. 但他却对这个unicode字符进行了decode,因为他以为读出来 ...

  8. 让NSURLConnection在子线程中运行

    可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...

  9. php大力力 [006节]初步接触认识phpMyAdmin

    phpMyAdmin 2015-08-22 php大力力006. 初步接触认识phpMyAdmin 以下是phpAdmin网络截图: 这是通过MAMP一键安装的. php中MyAdmin的使用-猿代码 ...

  10. BZOJ 3997 组合数学

    好厉害. 注意到到了(i,j)就一定到不了(i-1,j+1),那么可以dp啦.dp[i][j]表示(i,j)右上角都清了的方案数. #include<iostream> #include& ...