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. 学会WCF之试错法——客户端调用基础

    1当客户端调用未返回结果时,服务不可用(网络连接中断,服务关闭,服务崩溃等) 客户端抛出异常 异常类型:CommunicationException InnerException: Message: ...

  2. fc全连接层的作用、卷积层的作用、pooling层、激活函数的作用

    fc:1.起到分类器的作用.对前层的特征进行一个加权和,(卷积层是将数据输入映射到隐层特征空间)将特征空间通过线性变换映射到样本标记空间(也就是label) 2.1*1卷积等价于fc:跟原featur ...

  3. List环形双向链表

    实现一个环形的双向链表,链表的每个节点都保存三个信息,当前节点的值value,前一个节点的指针prev,后一个节点的指针next.因为是环形的,所以最后一个节点的next指向第一个节点,而第一个节点的 ...

  4. Java之生成Pdf并对Pdf内容操作

    虽说网上有很多可以在线导出Pdf或者word或者转成png等格式的工具,但是我觉得还是得了解知道是怎么实现的.一来,在线免费转换工具,是有容量限制的,达到一定的容量时,是不能成功导出的;二来,业务需求 ...

  5. js中数组的api整理

    首先列出所有的方法: join(), sort(), slice(), splice(), concat(), reverse(), push()+pop(), shift()+unshift(), ...

  6. 在Notepad++中使用文本对比插件

    目前Notepad++最新版是7.5.1,但很多插件仍然不能在64位版中使用,官网上是这么说的“Note that the most of plugins (including Plugin Mana ...

  7. sublime text3配置python开发环境(windows版)

    安装阶段: sublime text3的安装: 下载网址:https://www.sublimetext.com/ 下载完成后 ,点击安装即可. 安装Package Control: 点击 Tools ...

  8. ;(function($,window,document,undefined){})(jQuery,window,document)

    ;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...

  9. Hibernate 事务不回滚

    问题:               这几天在做开发时,发现事务不回滚了,Service是用AOP加的事务,数据库是MySql, 表全部是InnoDB:   方法回滚是采用spring的手动回滚:   ...

  10. vue2.0 接收url参数

    1) 路由配置传参方式在配置路由时 例如 "/firewall/authorize/:uid/:uname/:token"页面url为 http://XXX.com/firewal ...