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 ) p[ i ] = 1;
    int Count = 1;
    while ( Count <= 5842 ) {
        int min = F[ p[ 0 ] ]*n[ 0 ];
        int spa = 0;
        for ( int i = 1 ; i < 4 ; ++ i )
            if ( min > F[ p[ i ] ]*n[ i ] ) {
                min = F[ p[ i ] ]*n[ i ];
                spa = i;
            }
        if ( min > F[ Count ] )
            F[ ++ Count ] = min;
        ++ p[ spa ];
    }
    int N;
    while ( cin >> N && N ) {
        cout << "The " << N;
        if ( N%10 == 1 && N%100 != 11 )
            cout << "st";
        else if ( N%10 == 2 && N%100 != 12 )
            cout << "nd";
        else if ( N%10 == 3 && N%100 != 13 )
            cout << "rd";
        else
            cout << "th";
        cout << " humble number is " << F[ N ] << "." << endl;
    }
    return 0;
}
时间: 2024-10-03 22:43:55

zoj 1095 - Humble Numbers的相关文章

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.

hdu 1058 Humble Numbers

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

HDU 1058 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

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