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〗(how-to) fix k860/k860i buletooth.

    bluedroid.so for k860/k860i 1./media/Enjoy/AndroidCode/cm10.1/device/lenovo/stuttgart/bluetooth/blue ...

  2. 〖Linux〗ltib的使用帮助

    scue@Link:/home/work/ltib$ ./ltib --help This script is used to manage the building of BSPs with com ...

  3. 【BIEE】安装好BIEE后,修改默认登录页面不为QuickStart页面

    已经安装好了BIEE,但是发布了自己的资料库后,默认的登录页面为QuickStart,导致已登录就看到错误页面 现在进行如下修改即可 点击登录身份后的名字,例如我的是weblogic 选择[我的账户] ...

  4. 18-spring学习-利用Annotation配置AOP

    之前是通过配置完成aop操作,如果自己写的话,太麻烦了,可以使用基于annotation的配置完成. 第一步:打开AOP的annotation支持 加上一句话: <context:annotat ...

  5. Java 的抽象特性:抽象类与接口深度解析

    要点: 抽象类 接口 抽象类与接口的差别 一. 抽象 对于面向对象编程来说,抽象是它的四大特征之中的一个. 在Java中,能够通过两种形式来体现OOP的抽象:接口和抽象类. 接口和抽象类为我们提供了一 ...

  6. 让 Node.js 支持 ES6 的语法

    为了让 Node.js 支持 ES6 的语法, 需要使用 Babel. 安装 es-checker 在使用 Babel 之前 , 我们先检测一下当前 node 对 es6 的支持情况. 在命令行下执行 ...

  7. unity, water cube

    <纪念碑谷>里有一关开始是一个宝箱展开后里面有一个water cube,其中还有小鱼在游.如下截图: 因为我们知道<纪念碑谷>是unity做的,而现在正开始学unity,所以也 ...

  8. Android Studio 使用感受 错误解决

    刚到公司不久,公司叫我用Android studio软件,曾经一直在用的是eclipse ADT.突然接触到的新名词让我有点适应只是来. 好吧,既然是公司要求,肯定有它的道理.就从网上下载了它的安装包 ...

  9. 为什么在c语言中使用gets函数是危险的

      If you have code like this: char s[10]; gets( s ); and you type in more than 10 characters when th ...

  10. 3、Cocos2dx 3.0游戏开发找小三之搭建开发环境

    尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27107295 搭建开发环境 使用 Cocos2d- ...