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] << ".\n";
    }
    return 0;
}
posted @ 2013-09-26 23:39  lmei  阅读(215)  评论(0编辑  收藏  举报