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. 第三章.搭建MyBatis工程环境

    1.数据库的准备: 数据库: create DATABASE mybatis: 数据表: CREATE TABLE `user` ( `id` int(10) NOT NULL AUTO_INCREM ...

  2. jQuery Cookie操作cookie

    jQuery cookie下载地址:http://plugins.jquery.com/cookie/ 使用jquery.cookie.js依赖于jquery 基本用法:   1. 创建cookie ...

  3. [LuoguP2900] [USACO08MAR]土地征用(Land Acquisition)

    土地征用 (Link) 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的宽.比 ...

  4. C#实现双向链表

    原文:http://www.cnblogs.com/skywang12345/p/3561803.html#a33 没有C#版本的..是不是很方..不过图和说明很好,引用一下 双向链表 双向链表(双链 ...

  5. Select Sort

    package com.syd.sort; /** * Description: * ClassName:SelectSort * Package:com.syd.sort * Date:2018/6 ...

  6. Oracle 常用脚本

    ORACLE 默认用户名密码 sys/change_on_install SYSDBA 或 SYSOPER 不能以 NORMAL 登录,可作为默认的系统管理员 system/manager SYSDB ...

  7. Head First Java学习笔记

    1.基本概念 1.1.工作方式 源代码(.java)---编译器(执行javac程序)---产生字节码(.class与平台无关)---JAVA虚拟机(JVM,读取与执行字节码) 1.2.汇编语言是对基 ...

  8. Java并发包:AtomicBoolean和AtomicReference

      AtomicBoolean AtomicBoolean是一个读和写都是原子性的boolean类型的变量.这里包含高级的原子操作,例如compareAndSet().AtomicBoolean位于J ...

  9. c++11线程创建的三种方法

    一.用一个初始函数创建一个线程 直接看代码:注意c++在运行一个可执行程序的时候(创建了一个进程),会自动的创建一个主线程,这个主线程和进程同生共死,主线程结束,进程也就结束了. #include & ...

  10. ABAP术语-R/3 Repository Information System

    R/3 Repository Information System 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/11/1100076.ht ...