首页 > 其他 > 详细

hdu 1058 Humble Numbers【dp】

时间:2015-06-05 19:50:15      阅读:268      评论:0      收藏:0      [点我收藏+]

题目链接: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】

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!