hdu 1058

1 ->?
1 ->2=min(1*2,1*3,1*5,1*7)
1 ->2 ->3=min(2*2,1*3,1*5,1*7)
1 ->2 ->3 -> 4 = min(2*2,2*3,1*5,1*7)
1 ->2 ->3 -> 4 ->5= min(3*2,2*3,1*5,1*7)
状态转移方程

F(n)=min(F(i)*2,F(j)*3,F(k)*5,F(m)*7)(n>i,j,k,m)

特别的: i,j,k,m 只有在本项被选中后才移动

#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
const  int N=5850;
int f[N];
int main()
{
    int i,a,b,c,d;
    a = b = c = d = 1;
    for( i = 1 ; i  < 8 ; i++)
    f[i]=i;//前 7
    for( i = 8 ; i < 5850; i++)
    {
        while(2*f[a]<=f[i-1])a++;
        while(3*f[b]<=f[i-1])b++;
        while(5*f[c]<=f[i-1])c++;
        while(7*f[d]<=f[i-1])d++;
        f[i]= min(2*f[a],min(3*f[b],min(5*f[c],7*f[d])));
    }
    while(cin>>i,i)
    {
        cout<<"The "<<i;
        if(i % 10 == 1 && i %100 != 11)cout<<"st";
        else if(i %10 ==2 && i %100 != 12)cout<<"nd";
        else if( i %10 == 3 && i % 100!=13)cout<<"rd";
        else cout<<"th";
        cout<<" humble number is "<<f[i]<<"."<<endl;
    }
    return 0;
}
View Code

 

posted on 2013-07-31 20:11  blieveboy  阅读(64)  评论(0编辑  收藏  举报

导航