HDU1058——Humble Numbers

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

University of Ulm Local Contest 1996

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1159 1069 1087 1176 1421

挺好的一道题,对于每个a[c1]即该值已经用过了c1个2,看接下来加进去什么数

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[5900];
int min1(int a,int b,int c,int d)
{
    return min(min(min(a,b),c),d);
}
void inti()
{
    int c1,c2,c3,c4;
    c1 = c2 = c3 = c4 = 1;
    a[1] = 1;
    for(int i = 2; i <= 5842; i++){
        a[i] = min1(2*a[c1],3*a[c2],5*a[c3],7*a[c4]);
        if(a[i] == 2*a[c1]) c1++;
        if(a[i] == 3*a[c2]) c2++;
        if(a[i] == 5*a[c3]) c3++;
        if(a[i] == 7*a[c4]) c4++;
        }
}
int main()
{
    inti();
    int n;
    while(~scanf("%d",&n)&&n){
    if(n%100 == 11 || n%100 == 12 || n % 100 == 13){
        printf("The %dth humble number is %d.\n",n,a[n]);
    }
    else if(n%10 == 1){
        printf("The %dst humble number is %d.\n",n,a[n]);
    }
    else if(n%10 == 2){
        printf("The %dnd humble number is %d.\n",n,a[n]);
    }
    else if(n%10 == 3){
        printf("The %drd humble number is %d.\n",n,a[n]);
    }
    else printf("The %dth humble number is %d.\n",n,a[n]);
    }
    return 0;
}

  

时间: 2024-10-23 05:41:47

HDU1058——Humble Numbers的相关文章

hdu1058 Humble Numbers(丑数)

丑数:只包含一定的质因子的数称丑数,例如包含2,3,5.我们就把2,3,4,5,6,8,9,10,12,15........但我们通常把1称作为第一个丑数. 解题思路:我们现在做的这道题,就是以2,3,5,7为质因子,要我们求第n个丑数(以1为第一个丑数),可以采用DP的思想来解决.我们先以array[1]=1为基础,在它乘以质因子取得最小的数为有序数组的第二个,然后依次类推.写出状态转移方程:array[n] = Min(array[n2]*2, array[n3]*3, array[n5]*

HDU1058 Humble Numbers 【数论】

Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17407    Accepted Submission(s): 7565 Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble numb

HDU1058 Humble Numbers(DP)

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 pri

Humble Numbers(丑数) 超详解!

给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑数为 h[n]. 算法 1: 一种最容易想到的方法当然就是从 2 开始一个一个的判断一个数是否为丑数.这种方法的复杂度约为 O( k * h[n]),铁定超时(如果你这样做而没有超时,请跟 tenshi 联系) 算法 2: 看来只有一个一个地主动生成丑数了 : 我最早做这题的时候,用的是一种比较烂的

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

洛谷P2723 丑数 Humble Numbers [2017年 6月计划 数论07]

P2723 丑数 Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包括,p1.p1*p2.p1*p1.p1*p2*p3...(还有其 它).该集合被称为S集合的“丑数集合”.注意:我们认为1不是一个丑数. 题目描述 你的工作是对于输入的集合S去寻找“丑数集合”中的第N个“丑数”.所有答案可以用longint(32位整数)存储. 补充:丑数集合中每个数从小到大排列,每个丑

Humble Numbers HDU - 1058

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 i

Humble Numbers

Humble Numbers For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all numbers whose prime factors are a subset of S. This set contains, for example, p1, p1p2, p1p1, and p1p2p3 (among others). This is the set of `humble numb

(hdu step 3.2.5)Humble Numbers(从小到大输出因子只有2,3,,5,7的数)

题目: Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 913 Accepted Submission(s): 492   Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number.