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. LayIM项目之基础数据获取代码优化,Dapper取代ADO.NET

    前言 最近在开发LayIM融云版,也在进行项目重构,现在在看之前的代码,简直不敢直视.不过不知道以后看现在的代码是不是也是糟糕的一批.LayIM有个接口,一般接触过的开发人员都不会生疏,就是init接 ...

  2. ASP.NET SingalR + MongoDB 实现简单聊天室(一):搭建基本框架

    ASP.NET SingalR不多介绍.让我介绍不如看官网,我这里就是直接上源代码,当然代码还是写的比较简单的,考虑的也少,希望各位技友多多提意见. 先简单介绍聊天室功能: 用户加入聊天室,自动给用户 ...

  3. halcon 数字转字符串实现循环读取图片

    1.将字符转换为数字 tuple_number (StringImageIndex,IntImageIndex) 2.将数字转换为字符 tuple_string (IntImageIndex, '0' ...

  4. js秒换成天时分

    js秒换成天时分 function timeStamp( second_time ){ var time = parseInt(second_time) + "秒"; if( pa ...

  5. es6之decorator

    //decorator //第三方库为:core-decorators //以下为代码实例 { //decorator //修饰器是一个函数 //是修改一个行为 //修改一个类的行为 console. ...

  6. python-装饰器的简单使用

    一.介绍 首先我们先来看一个简单的例子,在基础平台中有一个home()和tv()函数,在业务平台中调用此函数时,给出了响应的打印内容: 基础平台: def home(): print('welcome ...

  7. NopCommerce 3.4中移动端访问抛弃响应式布局

    在Nop3.4中,他抛弃了原来的xxx.Mobile.cshtml的这种写法,而是采用了响应式布局,并且把规则也给改了,你在后台配置不启用响应式布局,在前台你仍然不能写xxx.Mobile.cshtm ...

  8. 【2017002】C#FTP上传文件

    //上传文件 public static Boolean FtpUpload(string ftpPath, string localFile, FtpServer svr) { //检查目录是否存在 ...

  9. 【C++ Primer】读书笔记_第一章

    Main(): 1. C++程序必须包含main()函数,操作系统通过调用main来运行C++程序. 2. main()的形参可以为空. 3. main函数的返回类型必须为int,返回给操作系统.in ...

  10. MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题

    参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...