Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30212    Accepted Submission(s): 13229

Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers.

Write a program to find and print the nth element in this sequence

 
Input
The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
 
Output
For each test case, print one line saying "The nth humble number is number.". Depending on the value of n, the correct suffix "st", "nd", "rd", or "th" for the ordinal number nth has to be used like it is shown in the sample output.
 
Sample Input
1
2
3
4
11
12
13
21
22
23
100
1000
5842
0
 
Sample Output
The 1st humble number is 1.
The 2nd humble number is 2.
The 3rd humble number is 3.
The 4th humble number is 4.
The 11th humble number is 12.
The 12th humble number is 14.
The 13th humble number is 15.
The 21st humble number is 28.
The 22nd humble number is 30.
The 23rd humble number is 32.
The 100th humble number is 450.
The 1000th humble number is 385875.
The 5842nd humble number is 2000000000.
 

题意:输入n,输出第n个丑数

题解:丑数是可以由2,3,5,7,因子组成的,应用丑数是2,3,5,7的倍数这一性质来做。用动态规划,从第一个丑数1开始,第二个丑数就是前面的丑数乘以2,3,5,7后最小的那个值,就是2。

 #include<bits/stdc++.h>
using namespace std;
long long a[];
int main() {
int num=;
int p2,p3,p5,p7;
p2=p3=p5=p7=;
a[]=;
while(num<) {
a[++num]=min(min(a[p2]*,a[p3]*),min(a[p5]*,a[p7]*));
if(a[num]==*a[p2])//找到之后就在下面用if语句把能得到当前
p2++; // 丑数的情况去掉 继续找下一个
if(a[num]==*a[p3])
p3++;
if(a[num]==*a[p5])
p5++;
if(a[num]==*a[p7])
p7++;
}
int n;
while(~scanf("%d",&n),n)
{
if(n%==&&n%!=)
printf("The %dst humble number is %lld.\n",n,a[n]);
else if(n%==&&n%!=)
printf("The %dnd humble number is %lld.\n",n,a[n]);
else if(n%==&&n%!=)
printf("The %drd humble number is %lld.\n",n,a[n]);
else
printf("The %dth humble number is %lld.\n",n,a[n]);
} return ;
}

hdu1058Humble Numbers(动态规划)的更多相关文章

  1. HDU1058Humble Numbers

     Humble Numbers Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u     ...

  2. Poj 1338 Ugly Numbers(数学推导)

    一.题目大意 本题要求写出前1500个仅能被2,3,5整除的数. 二.题解 最初的想法是从1开始检验该数是否只能被2,3,5整除,方法是这样的,对于一个数,如果它能被2整除,就除以2,如果它能被3整除 ...

  3. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  4. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  5. 【CF55D】Beautiful numbers(动态规划)

    [CF55D]Beautiful numbers(动态规划) 题面 洛谷 CF 题解 数位\(dp\) 如果当前数能够被它所有数位整除,意味着它能够被所有数位的\(lcm\)整除. 所以\(dp\)的 ...

  6. hdu 1058:Humble Numbers(动态规划 DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. ural 1013. K-based Numbers. Version 3(动态规划)

    1013. K-based Numbers. Version 3 Let’s consider K-based numbers, containing exactly N digits. We def ...

  8. 【 【henuacm2016级暑期训练】动态规划专题 K】 Really Big Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现如果x是reallynumber那么x+1也会是reallynumber.... (个位数+1,各位数的和+1了但是整个数也+ ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

随机推荐

  1. 你真的了解现在的PHP吗?

    前段时间,公司的项目从PHP5.3升级到PHP7,现在项目里开始使用PHP7的一些新语法和特性.反观PHP的5.4.5.5.5.6版本,有点认知缺失的感觉.所以,决定看<Modern PHP&g ...

  2. centos中java安装跟配置

    安装配置java环境 [root@JxateiLinux src]# Wget  http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c ...

  3. Learning by doing——获黄色领骑衫之感

    获奖感言 能拿到这件黄色的领骑衫,心里真的非常高兴.仔细看了一下,扣子.领子.各种图案各种细节十分精致.可以说这件领骑衫既有纪念意义,又有实用意义,真的很棒. 背后的故事 其实开始接触博客的时候,我是 ...

  4. 大数据框架-spark

    相关详细说明:https://www.csdn.net/article/2015-07-10/2825184 RDD:弹性分布式数据集. Operation:Transformation 和Actio ...

  5. windows下MySQL免安装版配置教程mysql-8.0.12-winx64.zip版本

    引用1:https://blog.csdn.net/weixin_42831477/article/details/81589325 引用2:https://blog.csdn.net/qq_3193 ...

  6. 我的名字叫hadoop

      第一回 新入环境 我的名字是hadoop,我一出生我的爸爸雅虎就给我取了这样一个名字:hadoop,我也不知道为什么叫这个名字,刚出生没多久,雅虎爸爸就把我领进一个黑暗的屋子里面,屋里堆满了黑色的 ...

  7. Django模板语言与视图(view)

    常用语法 {{  }}和{% %} 变量相关的用{{}} , 逻辑相关的用{% %} 变量 在Django的模板语言中按此语法使用:{{ 变量名 }}. 当模版引擎遇到一个变量,它将计算这个变量,然后 ...

  8. python新手第一天学习笔记-第一个ptyhon程序和python变量

    一.python 的注释和第一个python 程序 : 1.单行注释 # Author Xiajq 2.多行注释 ''' ------------注释内容----------------------- ...

  9. C、C++混合调用

    在项目中,C和C++代码相互调用是很常见的,但在调用时,究竟应该如何编写代码和头文件,有一些讲究,不然就可能出现编译时链接不通过的问题,典型的编译错误日志是: undefined reference ...

  10. 前端页面加载速度优化---Ngnix之GZIP压缩

    gzip on; #开启Gzip gzip_static on;#是否开启gzip静态资源 #nginx对于静态文件的处理模块,该模块可以读取预先压缩的gz文件,这样可以减少每次请求进行gzip压缩的 ...