hdoj 1058 Humble Number

主要的思想就是dp, 转移方程不大明显,但是比较容易想。 下一个丑数由之前的丑数乘{2,3,5,7}中最小且大于上一个数的数得到。先把所有数算出来然后打表输出。输出太恶心了,还考人英语。。


/*
PROG: humble number
LANG: C++11 
*/
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <time.h>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps  10e-8

typedef long long ll;
const int N = 100010;
const ll MOD = 1000000007;
const int INF = 0x7fffffff;

ll humble[6000];
int prime [4]= {2,3,5,7};

int main()
{
	int n, i, j, k;
    ll cur, minn;
    int maxn = 5842;
    humble[1] = 1;
    humble[2] = 2;
    for(i = 3; i <= maxn; i++){
        minn = INF;
        k = 1;
        for(j = 0; j < 4; j++){
            for(; k < i; k++){<span style="white-space:pre">			</span>// instead of making k = 1 every time
                cur = prime[j] * humble[i-k];   // because prime is in increasing order  
                if(cur <= humble[i-1])
                    break;
                minn = min(minn, cur);
            }
        }
        humble[i] = minn;
    }
    while(scanf("%d", &n)){
        if(!n) 
            break;
        cout << "The " << n;
        if(n%10 ==1 && n%100 != 11)
            cout << "st humble number is ";
        else if(n%10 == 2 && n%100 != 12)
            cout << "nd humble number is ";
        else if(n%10 ==3 && n%100 != 13)
            cout << "rd humble number is ";
        else
            cout << "th humble number is ";
        cout << humble[n]  << "." << endl;
    }
    return 0;
}

中间剪枝了一下,比网上流行的算法稍微快一点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值