Humble Numbers

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

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.
  
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1069 1087 1176 1024 1203 

 
  简单动态规划问题。
  思路是后面的Humble数总可以用前面求得的某个Humble数乘以{2,3,5,7}中的一个求得。
  需要注意的是输出需要按序数的规则输出,个位数是1、2、3的后缀分别是st、nd、rd,但是后两位为11、12、13的数例外(例如11、12、13、111、112、113),它们的后缀是th。其余数后缀均为th。
 
 #include <iostream>
using namespace std;
int a[];
int Min(int a,int b,int c,int d)
{
int t;
t = a<b?a:b;
t = t<c?t:c;
t = t<d?t:d;
return t;
}
int main()
{
int n;
a[] = ;
int a1=,a2=,a3=,a4=;
int e1,e2,e3,e4;
for(int i=;i<;i++){
e1 = a[a1]*;
e2 = a[a2]*;
e3 = a[a3]*;
e4 = a[a4]*;
a[i] = Min(e1,e2,e3,e4);
if(a[i]==e1)
a1++;
if(a[i]==e2)
a2++;
if(a[i]==e3)
a3++;
if(a[i]==e4)
a4++;
}
while(cin>>n){
if(n==)
break;
if(n%== || n%== || n%==)
cout<<"The "<<n<<"th humble number is "<<a[n]<<'.'<<endl;
else {
int t = n%;
switch(t){
case :cout<<"The "<<n<<"th humble number is "<<a[n]<<'.'<<endl;break;
case :cout<<"The "<<n<<"st humble number is "<<a[n]<<'.'<<endl;break;
case :cout<<"The "<<n<<"nd humble number is "<<a[n]<<'.'<<endl;break;
case :cout<<"The "<<n<<"rd humble number is "<<a[n]<<'.'<<endl;break;
default:cout<<"The "<<n<<"th humble number is "<<a[n]<<'.'<<endl;break;
}
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1058:Humble Numbers(动态规划 DP)的更多相关文章

  1. HDU 1058 Humble Numbers【DP】

    题意:给出丑数的定义,只含有2,3,5,7这四个素数因子的数称为素数.求第n个丑数. 可以先观察几个丑数得出规律 1:dp[1] 2:min(1*2,1*3,1*5,1*7) 3:min(2*2,1* ...

  2. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  3. HDU 1058 Humble Numbers (DP)

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

  4. HDU 1058 Humble Numbers (动规+寻找丑数问题)

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

  5. HDU 1058 Humble Numbers(离线打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有58 ...

  6. hdu 1058 Humble Numbers

    这题应该是用dp来做的吧,但一时不想思考了,写了个很暴力的,类似模拟打表,然后排序即可,要注意的是输出的格式,在这里wa了一发,看了别人的代码才知道哪些情况没考虑到. #include<cstd ...

  7. hdu 1058 Humble Numbers(构造?枚举?)

    题意: 一个数的质因子如果只是2,3,5,7中的若干个.则这个数叫做humble number. 例如:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 1 ...

  8. HDU 1058 Humble Number

    Humble Number Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humbl ...

  9. HDOJ 1058 Humble Numbers(打表过)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

随机推荐

  1. Android-PullToRefresh(一)

    先讲下这篇写啥东西,也就是这家伙(chrisbanes)写的一个上拉下拉刷新的Demo,连接https://github.com/fengcunhan/Android-PullToRefresh 东西 ...

  2. CompletableFuture 详解

    转 http://www.jianshu.com/p/6f3ee90ab7d3 CompletableFuture类实现了CompletionStage和Future接口.Future是Java 5添 ...

  3. Jenkins部署java项目实例02

    源码管理 选择 “git”, Repository URL填写你的git地址,并配置对应的key,大家最好事前先在jenkins这台机器上能够直接远程git操作私有仓库 构建触发器.构建环境.Pre ...

  4. 去掉A标签的虚线框

    outline是css3的一个属性,用的很少. 声明,这是个不能兼容的css属性,在ie6.ie7.遨游浏览器都不兼容. outline控制的到底是什么呢? 当聚焦a标签的时候,在a标签的区域周围会有 ...

  5. Windows下Python添加库(模块)路径

    动态的添加库路径.在程序运行过程中修改sys.path的值,添加自己的库路径 import syssys.path.append(r'your_path') 在Python安装目录下的\Lib\sit ...

  6. session过期后iframe页面如何跳转到parent页面

    session过期后如果在iframe里操作就会返回到login.html,可是这个页面还在iframe里面如果再次登陆就会出现iframe嵌套的现象,我们这样来解决. 在login.html里面加上 ...

  7. 《转》CentOS7 安装MongoDB 3.0server (3.0的优势)

    1.下载&安装 MongoDB 3.0 正式版本号公布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活并且易于管理的数据库管理系统.MongoDB宣称.3.0新版本号 ...

  8. JavaScript之Web通讯

    web通信,一个特别大的topic,涉及面也是很广的.因最近学习了 javascript 中一些 web 通信知识,在这里总结下.文中应该会有理解错误或者表述不清晰的地方,还望斧正! 一.前言 1. ...

  9. Linux下编译、使用静态库和动态库 自己测过的

    每个程序实质上都会链接到一个或者多个的库.比如使用C函数的程序会链接到C运行时库,GUI程序会链接到窗口库等等.无论哪种情况,你都会要决定是链接到静态库(static libary)还是动态库(dyn ...

  10. CakePHP不支持path/to路径,前后台无法方法

    本来想把前后台分离,可是阅读了cakephp的说明,才发现.cakephp根本就不支持path/to路径. cakephp官网给出的 管理员分离方式:http://book.cakephp.org/2 ...