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. (一)Hibernate初探之——单表映射

    Hibernate充当持久化层 项目结构: 一.创建项目导入jar包. hibernate-core   &  junit4       &   mysql-jdbc 二.src目录下 ...

  2. Ubuntu下安装使用Xfce4

    编辑于 2007-05-05 21:30   安装:  代码: sudo  apt-get  install  xfce4  xfce4-taskbar-plugin     (xfce4-taskb ...

  3. mysql优化三

    1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...

  4. Directshow开发播放器相关介绍

    原文地址:http://www.cnblogs.com/qiufa/archive/2006/12/19/596949.html DirectShow技术是DirectX推出的建立在DirectDra ...

  5. MySQL主从不一致的几种故障总结分析、解决和预防

    (1).主从不一致故障,从库宕机,从库启动后重复写入数据报错解决与预防:relay_log_info_repository=TABLE(InnoDB)参数解释说明:若relay_log_info_re ...

  6. Oracle10g 安装步骤

    一.Oracle10g 安装预备步骤 取得 Oracle 10g 安装程序,或从 Oracle 技术网(OTN)下载光盘映像.在评估阶段您可以免费下载和使用无技术限制的全功能 Oracle,但在正式的 ...

  7. Mysql使用大全 从基础到存储过程

    平常习惯了phpmyadmin等其他工具的的朋友有的根本就不会命令,如果让你笔试去面试我看你怎么办,所以,学习一下还是非常有用的,也可以知道你通过GUI工具的时候工具到底做了什么.Mysql用处很广, ...

  8. linux kill 关闭进程命令

    杀死进程最安全的方法是单纯使用kill命令,不加修饰符,不带标志. 首先使用ps -ef命令确定要杀死进程的PID,然后输入以下命令: # kill -pid 注释:标准的kill命令通常都能达到目的 ...

  9. 高性能网络 | 你所不知道的TIME_WAIT和CLOSE_WAIT

    你遇到过TIME_WAIT的问题吗?   我相信很多都遇到过这个问题.一旦有用户在喊:网络变慢了.第一件事情就是,netstat -a | grep TIME_WAIT | wc -l 一下.哎呀妈呀 ...

  10. App Extension的脱壳办法

    App Extension的脱壳办法 从app store下载的app和app extension是加过密的,可以通过otool查看: $ otool -l binary_name | grep cr ...