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. iOS-仿智联字符图片验证码

    概述 随机字符组成的图片验证码, 字符位数可改变, 字符可斜可正排列. 详细 代码下载:http://www.demodashi.com/demo/10850.html 项目中有时候会有这种需求: 获 ...

  2. Idea 翻译插件

    https://github.com/YiiGuxing/TranslationPlugin

  3. 工作总结 表单提交中 Input 设置 disabled readonly

    input框里面添加disabled属性之后,该内容就无法向上提交  需要的时候也可以再移除disabled readonly这个属性来禁止用户修改, 可以正常提交. Hiddent 隐藏  也可以正 ...

  4. 查看nginx的版本

    查看nginx的版本 -v 显示 nginx 的版本-V 显示 nginx 的版本,编译器版本和配置参数 # /app/nginx/sbin/nginx -v nginx version: nginx ...

  5. C#位操作

    一.原码与补码 在计算机系统中,数值一律用补码来存储(表示).主要原因:使用补码,可以将符号位和其他位统一处理:同时减法也可按加法来处理.另外,两个补码表示的数相加时,如果最高位(符号位)有进位,则进 ...

  6. ICSharpCode.SharpZipLib工具压缩与解压缩zip文件

    using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCod ...

  7. Java 枚举(enum) 的常见用法和开发规范

    JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 用法一:常量 在JDK1.5 之前,我们定义常量都是: public static final ...

  8. [svc]linux测速软件speedtest-cli

    linux测速软件: https://github.com/sivel/speedtest-cli 以下是我在gentoo上测的

  9. xa

    题目描述把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法.输入每个用例包含二个整数M和N.0<=m<=1 ...

  10. CEffectMgr类

    #ifndef __EFFECTMGR_H__ #define __EFFECTMGR_H__ #include "GameFrameHead.h" namespace cocos ...