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. Myeclipse 快捷键设置和自动提示设置

    1.快捷键设置和调整 2.自动提示信息设置与调整

  2. BUILD_BUG_ON 的解释

    知乎上个问题<C 语言有什么奇技淫巧?>排名第一的是一个“抖机灵”的答案. C有一个鲜为人知的运算符叫”趋向于”, 写作“-->”.比如说如果要实现一个倒数的程序,我们可以定义一个变 ...

  3. 比较器comparable与comparator的使用

    在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...

  4. synchronized(this) 和synchronized(xxx.class)的区别和联系

    synchronized(ThreadTest.class)是对ThreadTest这个类进行加锁,类里面的属性,方法都是同步的,是针对于特定的类的~~ synchronized(this){}是对{ ...

  5. Galera 10.0.20 on CentOS 6.6

    Galera 10.0.20 on CentOS 6.6 0.使用场景 数据库软件:mariadb-galera-10.0.20-linux-x86_64.tar.gz 集群管理:galera-3-2 ...

  6. 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...

  7. Educational Codeforces Round 2 B. Queries about less or equal elements 水题

    B. Queries about less or equal elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforc ...

  8. Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题

    B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...

  9. Parallax Mapping Shader 凸凹感【转】

    原文 http://www.azure.com.cn/default.asp?cat=11&page=2 Parallax Mapping 就是通过高度图中的高度,对纹理坐标进行偏移,来视觉上 ...

  10. HDU 4293 Groups (线性dp)

    OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多 ...