ACM -- 算法小结(九)DP之Humble numbers

     DP -- Humble numbers 

//一开始理解错题意了,题意是是说一些只有唯一一个质因数(质因数只包括2,3,5,7)组成的数组,请找出第n个数是多少
//无疑,先打表,否则果断超时

#include <iostream>
using namespace std;
int a[4];
int b[4] = {2, 3, 5, 7};
int ans[5842];
int main()
{
    int i,j;
    for(i = 0; i < 4; i++)
        a[i] = 0;
    ans[0] = 1;
    for(i = 1; i < 5842; i++)
    {
        int tmp = 0;
        for(j = 1; j < 4; j++)
        {
            if(ans[a[j]] * b[j] < ans[a[tmp]] * b[tmp])
                tmp = j;
        }
        ans[i] = ans[a[tmp]] * b[tmp];
        for(j = 0; j < 4; j++) 
        {
            if(ans[i] == ans[a[j]] * b[j])
                a[j]++;
        }
    }

    int n;
    while(cin >> n && n != 0)
    {
        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 ";
        cout << ans[n - 1] << ".
";
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lmei/p/3341973.html