首页 > 代码库 > zoj 1095 - Humble Numbers

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;
}

zoj 1095 - Humble Numbers