状态转移方程:f[i]=min(f[x]*2+f[y]*3+f[p]*5+f[q]*7);这题如果不打表超时,不懂为啥?
#include<iostream>
using namespace std;
int f[6000];
int min(int a,int b,int c,int d,int *q)
{
if(a<=b&&a<=c&&a<=d)
{
*q=1;return a;
}
else
if(b<a&&b<=c&&b<=d)
{*q=2;return b;}
else
if(c<a&&c<b&&c<=d)
{
*q=3;return c;
}
else
{
*q=4;return d;
}
}
int main()
{
int i,k,j;
memset(f,0,sizeof(f));
for(i=1;i<=1;i++)
{
f[i]=i;
}
int n;
int x,y,q,p;
int temp=0;
x=y=p=q=1;
for(i=2;i<=5842;i++)
{
f[i]=min(f[x]*2,f[y]*3,f[p]*5,f[q]*7,&temp);
if(f[i]==f[i-1])
i--;
switch(temp)
{
case 1:x++;break;
case 2:y++;break;
case 3:p++;break;
case 4:q++;break;
}
}
while(cin>>n&&n!=0)
{
int mod;
if(n%100>=10&&n%100<=20)
mod=4;
else
mod=n%10;
switch(mod)
{
case 1:
cout<<"The "<<n<<"st humble number is "<<f[n]<<"."<<endl;break;
case 2:
cout<<"The "<<n<<"nd humble number is "<<f[n]<<"."<<endl;break;
case 3:
cout<<"The "<<n<<"rd humble number is "<<f[n]<<"."<<endl;break;
default:
cout<<"The "<<n<<"th humble number is "<<f[n]<<"."<<endl;break;
}
}
return 0;
}