码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1058 Humble Numbers【dp】

时间:2015-06-05 19:53:25      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:dp

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1058

题意:数字只有2,3,5,7素因子的数叫做Humble Number,问你第 n 个Humble Number是什么?

解法:枚举。

代码:

#include <stdio.h>
#include <string.h>
#include <vector>  
#include <string>  
#include <algorithm>  
#include <iostream>
#include <iterator>
#include <fstream>
#include <set>
#include <map>
#include <math.h>

using namespace std;

int n;
int p[6000];
int tmp[5] = { 2, 3, 5, 7 };
set<long long> SET;

void init()
{
    p[1] = 1;
    int n = 1, m = 1, k = 1, l = 1;
    for (int i = 2; i <= 5842; i++)
    {
        int a = p[n] * 2;
        int b = p[m] * 3;
        int c = p[k] * 5;
        int d = p[l] * 7;
        int ans = min(a,min(b,min(c,d)));
        if (ans == a) n++;
        if (ans == b) m++;
        if (ans == c) k++;
        if (ans == d) l++;
        p[i] = ans;
    }
}

int main()
{
    init();
    while (scanf("%d",&n)!=EOF && n)
    {
        printf("The %d",n);

        if (n % 100 == 13 || n % 100 == 12 || n % 100 == 11)
            printf("th");
        else if (n % 10 == 1) printf("st");
        else if (n % 10 == 2) printf("nd");
        else if (n % 10 == 3) printf("rd");
        else printf("th");

        printf(" humble number is %d.\n",p[n]);
    }
    return 0;
}

hdu 1058 Humble Numbers【dp】

标签:dp

原文地址:http://blog.csdn.net/u014427196/article/details/46379515

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!