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. Java开发环境配置(Win7 64位系统/server 2008)

    下面以jdk1.8.0_05版本为例: 1.在用户变量里新增变量名:JAVA_HOME 变量值:D:\Java\jdk1.8.0_05 (根据实例路径变换) 2.在用户变量里新增变量名:CLASSPA ...

  2. Tomcat 关闭时报错

    最近tomcat走普通的关闭方式无法正常关闭,会报一些Error,用的是Tomcat7,据说是Tomcat7在关闭的时候加了一些检查线程泄漏内存泄露的东西 总结起来,在我项目中有这么几个原因会导致关闭 ...

  3. 导入项目出现: Unable to resolve target ‘android-10′ 解决办法

    进入到android项目根目录下,打开项目文件project.properties ,修改 target=android-10  的值.把10改为当前虚拟机API level的版本即可.我这里改为17 ...

  4. EMQ配置

    参考官方配置页面:http://emqtt.com/docs/v2/tune.html Linux 操作系统参数系统所有进程可打开的文件数量官方配置: sysctl -w fs.file-max=20 ...

  5. 流类库继承体系(IO流,文件流,串流)和 字符串流的基本操作

    一.IO.流 数据的输入和输出(input/output简写为I/O) 对标准输入设备和标准输出设备的输入输出简称为标准I/O 对在外存磁盘上文件的输入输出简称为文件I/O 对内存中指定的字符串存储空 ...

  6. 值得分享的Bootstrap Ace模板实现菜单和Tab页效果(转)

    Ace模板地址:http://code.google.com/p/ace-engine/wiki/AceTemplate(有时会打不开) Ace英文官网:http://wrapbootstrap.co ...

  7. centos gnome桌面放大

    我不知道gnome 为什么要这么做.但是真的有效: gsettings set org.gnome.desktop.interface scaling-factor # 放大2倍

  8. Eclipse的vim插件viPlugin的安装

    1.viPlugin是什么?   viPlugin是一个eclipse 针对vi的插件,使用此插件可以让你在使用eclipse进行编码时使用几乎所有vi命令,可以极大的提高开发编码效率. 2.viPl ...

  9. 快速创建各种类型的NSAttributeString和NSMutableParagraphStyle

      NSDictionary *attributes = @{ NSForegroundColorAttributeName : [ UIColorredColor ], NSFontAttribut ...

  10. (2)FluidMoveBehavior 之单击 Grid 中 Tile 进行排序

    在上一篇文章中,使用 FluidMoveBehavior 结合 FluidMoveSetTagBehavior 可以使数据从 ListBox 中的 数据显示时,产生缓慢的动画,从而更加生动.其实 Fl ...