HDU 1058 Humble Numbers (DP)

时间:2022-08-28 23:11:46

Humble Numbers

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

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.
 

说DP有点牵强,不过也算是用前面的状态推出后面的状态,英语知识是个易错点。

思路:2,5,7,9是丑数,丑数的2,5,7,9倍也是丑数,所以从1开始,对每个数计算计算它的2,5,7,9倍,算出来的结果又再翻倍。难免会遇到这种考察其它乱七八糟的知识点的题,输出时的后缀很容易错,在英语里如果一个数的个位是1,那后面跟st,2跟nd,3跟rd,其它跟th,但是注意,后面两位是11,12,13的除外,这些数后面都跟th,比如12,10013,1111,我WA了三次,算是英语差的后果吧。

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 5845 int my_min(int a,int b,int c,int d);
int main(void)
{
int n;
int s[MAX] = {};
int i,i_2,i_3,i_5,i_7;
int box,box_2,box_3,box_5,box_7;
char cha[];
i = i_2 = i_3 = i_5 = i_7 = ; while(i < MAX - )
{
box_2 = s[i_2] * ;
box_3 = s[i_3] * ;
box_5 = s[i_5] * ;
box_7 = s[i_7] * ; box = my_min(box_2,box_3,box_5,box_7); //每次把最小的那个插入到数组里,就不用排序了
if(box == box_2) //要一直用if,因为结果可能相同
i_2 ++;
if(box == box_3)
i_3 ++;
if(box == box_5)
i_5 ++;
if(box == box_7)
i_7 ++; i ++;
s[i] = box;
} while(scanf("%d",&n) && n)
{
box = n % ;
if(box == || box == || box == )
{
strcpy(cha,"th");
goto label;
} box = n;
while(box / )
box %= ;
switch(box % )
{
case :strcpy(cha,"st");break;
case :strcpy(cha,"nd");break;
case :strcpy(cha,"rd");break;
default :strcpy(cha,"th");break;
} label:
printf("The %d%s humble number is %d.\n",n,cha,s[n - ]);
} return ;
} int my_min(int a,int b,int c,int d)
{
int min = a; min = min < b ? min : b;
min = min < c ? min : c;
min = min < d ? min : d; return min;
}

HDU 1058 Humble Numbers (DP)的更多相关文章

  1. HDOJ&lpar;HDU&rpar;&period;1058 Humble Numbers &lpar;DP&rpar;

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

  2. hdu 1058&colon;Humble Numbers(动态规划 DP)

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

  3. HDU 1058 Humble Numbers &lpar;动规&plus;寻找丑数问题&rpar;

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

  4. 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* ...

  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. &lbrack;poj2247&rsqb; Humble Numbers &lpar;DP水题&rpar;

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

  9. HDU 1058 Humble Number

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

随机推荐

  1. Linux 小命令

    查看 cat  文件名     [查看里面的内容,cate,猫,像猫一样瞄一眼的看] more 文件名   [查看文件,文件太多,一次看不完,用 more 来查看 下一页:空格或 f   下一行:回车 ...

  2. jiajianhao

    #include<stdio.h> int map[4][4]={ 0,1,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0}; int flag=0; int minci=9 ...

  3. BZOJ3068 &colon; 小白树

    枚举每条树边,将其断开,那么两侧肯定取带权重心最优. 考虑如何求出每个子树的重心,枚举其所有儿子,通过重量关系就可以判断出重心位于哪棵子树. 然后将那棵子树的重心暴力往上爬即可,因为每个点作为重心肯定 ...

  4. spring编程式刷新&sol;重新加载applicationcontext&sol;dispatchservlet(正确版)

    有些时候,尤其是在开发应用框架的时候,由于某些原因无法或者很难重启tomcat或者reload应用,但是配置又需要动态生效,这个时候通常希望通过reload spring applicationcon ...

  5. iOS JSON解析

    解析json成dic对象 -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error; NSDi ...

  6. Drupal 7 模 &period;info 文件描述

    Drupal使用.info文件以节省话题(theme)和模块(modules)基本数据(metadata). 编码:推荐使用UTF-8.这是一个没有BOM(Byte Order Mark). 下面是一 ...

  7. tomcat启动异常、和web&period;xml缺少配置异常

    错误如下: 14-Feb-2017 10:50:00.665 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.Sta ...

  8. UNDO及MVCC、崩溃恢复

    UNDO特性:避免脏读.事务回滚.非阻塞读.MVCC.崩溃恢复 事务工作流程(图2) MVCC原理机制 崩溃恢复:redo前滚.undo回滚 长事务.大事务:危害.判断.处理 UNDO优化:实现und ...

  9. Windows Server 2012 R2 安装密钥(只适用安装,不支持激活)

    标准版 = NB4WH-BBBYV-3MPPC-9RCMV-46XCB 数据中心版 = BH9T4-4N7CW-67J3M-64J36-WW98Y

  10. Spring 12 种 常用注解&excl;

    1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller ...