HDU 1058 Humble Numbers 动态规划 或 暴力

题目大意:

就是求第n个满足可以由任意个2,3,5,7相乘能得到的正整数

注意输出时的格式即可


大致思路:

这题首先明显能暴力枚举=_=,这是一个方法

好一点的可以知道用dp[ i ] 表示第 i 个满足条件的数是 dp[ i ] 那么dp[ i ] 肯定来自于前面的数乘上2或者3,5,7这样注意一下更新条件即可

代码如下:

Result  :  Accepted     Memory  :  308 KB     Time  :  250 ms

/*
 * Author: Gatevin
 * Created Time:  2014/8/9 20:03:37
 * File Name: hehe.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

lint dp[5845];
int n;
int main()
{
    memset(dp, 0, sizeof(dp));
    for(int i = 1; i <= 10; i++) dp[i] = i;
    for(int i = 11; i <= 5842; i++)
    {
        dp[i] = 2*dp[i - 1];
        for(int j = i - 2; j >= 1; j--)
        {
            if(dp[j] * 7 <= dp[i - 1]) break;
            else dp[i] = min(dp[i], dp[j] * 7);
            if(dp[j] * 5 <= dp[i - 1]) continue;
            else dp[i] = min(dp[i], dp[j] * 5);
            if(dp[j] * 3 <= dp[i - 1]) continue;
            else dp[i] = min(dp[i], dp[j] * 3);
            if(dp[j] * 2 <= dp[i - 1]) continue;
            else dp[i] = min(dp[i], dp[j] * 2);
        }
    }
    char s1, s2;
    while(scanf("%d", &n) && n)
    {
        switch(n % 10)
        {
            case 1 : s1 = 's'; s2 = 't'; break;
            case 2 : s1 = 'n'; s2 = 'd'; break;
            case 3 : s1 = 'r'; s2 = 'd'; break;
            default : s1 = 't';s2 = 'h';
        }
        if(n % 100 == 11 || n % 100 == 12 || n % 100 == 13)
        {
            s1 = 't';
            s2 = 'h';
        }
        printf("The %d%c%c humble number is %I64d.\n", n, s1, s2, dp[n]);
    }
    return 0;
}


你可能感兴趣的:(动态规划,HDU,Numbers,1058,Humble)