humble number(hd1058)

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.

 1 /*看题解AC的,结果开始开编译错误了几次,oj说是min()没定义,那为什么自己编译会通过,然后自己写了min(),dp方程 dp[i]=f[i]=min(f[a]*2,min(f[b]*3,min(f[c]*5,f[d]*7)))*/
 2 #include<stdio.h>/*dev编译后运行程序中途闪退,提交后却对的*/
 3 using namespace std;
 4 int min(int a,int b)
 5 {
 6     return a>b?b:a;
 7 }
 8 int humble[5843];
 9 void factor()/*马丹看了半天也是似懂非懂,比赛时完全想不出来这么求*/
10 {
11     int post1=1,post2=1,post3=1,post4=1,na,nb,nc,nd,i;
12     humble[1]=1;
13     for(i=2;i<=5843;i++)
14     {
15         humble[i]=min(na=humble[post1]*2,min(nb=humble[post2]*3,min(nc=humble[post3]*5,nd=humble[post4]*7)));
16         if(humble[i]==na) post1++;
17         if(humble[i]==nb) post2++;
18         if(humble[i]==nc) post3++;
19         if(humble[i]==nd) post4++;
20     }
21 }
22 int main()
23 {
24     int n;
25     factor();
26     while(~scanf("%d",&n))
27     {
28         if(n==0)
29             break;
30         printf("The %d",n);
31         if(n%100!=11&&n%10==1)
32             printf("st");
33         else if(n%100!=12&&n%10==2)
34             printf("nd");
35         else if(n%100!=13&&n%10==3)
36             printf("rd");
37         else
38             printf("th");
39         printf(" humble number is %d.\n",humble[n]);
40     }
41 }
时间: 2024-10-28 23:23:51

humble number(hd1058)的相关文章

hdoj 1492 The number of divisors(约数) about Humble Numbers 【数论】【质因子分解 求和】

定理:一个正整数 n 可以用素因子唯一表示为 p1^r1 * p2^r2 * ... pk^rk (其中 pi 为素数) , 那么这个数的因子的个数就是,(r1+1)*(r2+1)*...*(rk+1). 理解:为什么是加1之后再相乘,因为一个数的的因子数至少为1和他自身,但因为r1,r2..可以为0,所以因子的个数为(r1+1)... 拓展一下: 定理1: 一个正整数 n 可以用素因子唯一表示为 p1^r1 * p2^r2 * ... pk^rk (其中 pi 为素数) , 那么这个数的因子的

HDU - The number of divisors(约数) about Humble Numbers

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. Now given a humble number, please write

HDU 1492 The number of divisors(约数) about Humble Numbers 数论

The number of divisors(约数) about Humble Numbers Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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,

HDU 1492 The number of divisors(约数) about Humble Numbers(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1492 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 firs

The number of divisors(约数) about Humble Numbers

The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3033    Accepted Submission(s): 1465 Problem Description A number whose only prime factors are 2,3

zoj 1095 - Humble Numbers

题目:找到用2,3,5,7为数的因第n个数字. 分析:dp,数据结构.类似于合并果子的队列维护,用4根指针分别指向队列,用当前的数乘以对应数字即可. 说明:(2011-09-19 01:42) #include <iostream> using namespace std; int F[ 5845 ]; int n[ 4 ] = {2,3,5,7}; int p[ 4 ]; int main() { F[ 1 ] = 1; for ( int i = 0 ; i < 4 ; ++ i )

poj 2247 Humble Numbers

Humble Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9453   Accepted: 4440 题目大意:找出所有因子中只有2,3,5,7的数,给出n,求出第n个这样   只有6000不到打表 注意第11  12  13 的输出与 1   2   3    几十一   几十二   几十三 的输出不同 #include <iostream> #include <algorithm> u

HDU 丑数 - 1058 Humble Numbers

这一题是讲了一个名叫丑数的概念(为啥叫丑数,,). 概念:因子中仅仅包含2.3.5,7的数,称为丑数.但其实我百度网上时,发现正常的丑数应该是因子中仅仅包含2.3.5,不过基本都一样. 我们可以通过不断mod2,3,5,7,直到无法在摸,验证此时是否为1来判断该数是否为丑数,但是这样的方法太过浪费时间,所以介绍一种新的方法,下面方法摘自这里点击打开链接. 根据丑数的定义,丑数应该是另一个丑数乘以2.3,5或者7的结果(1除外).因此我们可以创建一个数组,里面的数字是排好序的丑数.里面的每一个丑数

HDU 1058 Humble Numbers--DP--(计数器技巧)

题意:输出第n大的质因数只有2,3,5,7的数 分析:预处理满足这些条件的数,然后输出dp[n]就行,满足条件的数无非就是2,3,5,7不断的做乘积 这题的关键就是如何把这些乘积从小到大存入数组:设置四个计数器p2,p3,p5,p7完成这个任务 代码: #include<iostream> #include<cstdio> #define min(a,b) (a<b?a:b) using namespace std; int n,dp[6000]; void check()