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(HDU).1058 Humble Numbers (DP)

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

  2. hdu 1058: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 (动规+寻找丑数问题)

    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. [poj2247] Humble Numbers (DP水题)

    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. 第二百七十七天 how can I 坚持

    开玩笑要有个度,哎,或许这就是缘分,很容易受别人影响吗? 中国人为什么会经常抱怨,不抱怨,挺好. 睡觉,红颜祸水,老婆是要能一起 生活的,不是失去,是上天在帮我,哈哈.

  2. 【转】强大的vim配置文件,让编程更随意

    原文地址:http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主 ...

  3. ucGUI 12864 从打点起

      ucGUI是纯C写的的,移植需要定义点阵数,颜色数,和画点函数 以下是ucGUI 12864下的移植 基于ST7920控制的12864液晶用于字符显示很方便的,但网友说用它显示图形并不合适,原因就 ...

  4. mahout算法源码分析之Itembased Collaborative Filtering(二)RowSimilarityJob

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 本篇开始之前先来验证前篇blog的分析结果,编写下面的测试文件来进行对上篇三个job的输出进行读取: p ...

  5. PHP再学习1——cURL表单提交、HTTP请求和响应分析

    1.前言 最近迷恋WEB方面的技术,虽然自己是一个嵌入式工程师,但是我深知若需要把传感器终端的数据推送至“平台”必然会和WEB技术打交道.在工作中发现嵌入式工程师喜欢 二进制形式的协议,例如MODBU ...

  6. IOC使用Unity 实现依赖注入

    转自:http://www.cnblogs.com/techborther/archive/2012/01/06/2313498.html http://www.cnblogs.com/xishuai ...

  7. 窗体 dialog 弹出时动画效果

    1.先创建 anim中的 xml  动画文件 <?xml version="1.0" encoding="utf-8"? >   <set x ...

  8. 查看数量linux下查看cpu物理个数和逻辑个数

    首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 hadoop@chw-desktop3:~$ cat /proc/cpuinfo processor : 0 vendor_id : Gen ...

  9. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  10. MyBatis之六:缓存

    MyBatis 3中的缓存实现的很多改进都已经实现了,使得它更加强大而且易于配置.默认情况下是没有开启缓存的,除了局部的session缓存,可以增强变现而且处理循环依赖也是必须的.要开启二级缓存,你需 ...