如果一个数是Humble Number,那么它的2倍,3倍,5倍,7倍仍然是Humble Number
定义F[i]为第i个Humble Number
F[n]=min(2*f[i],3*f[j],5*f[k],7*f[L]), i,j,k,L在被选择后相互移动(移动方法:被选择的数,下标加1)
定义F[i]为第i个Humble Number
F[n]=min(2*f[i],3*f[j],5*f[k],7*f[L]), i,j,k,L在被选择后相互移动(移动方法:被选择的数,下标加1)
(通过此题理解到数组有序特性)
#include <iostream>
using namespace std;
const int MAXN = 5842;
int h[MAXN+4];
int main()
{
int i;
int i1,i2,i3,i4;
int t1,t2,t3,t4;
h[1] = 1;
i1=i2=i3=i4=1;
for(i=2;i<=MAXN;i++)
{
t1 = h[i1]*2;
t2 = h[i2]*3;
t3 = h[i3]*5;
t4 = h[i4]*7;
h[i]=min(t1,min(t2,min(t3,t4)));
if(h[i]==t1) i1++;
if(h[i]==t2) i2++;
if(h[i]==t3) i3++;
if(h[i]==t4) i4++;
// t1 , t2, t3, t4中可能有相等的,所以不用else if
}
int n;
while(scanf("%d",&n)!=EOF && n)
{
printf("The %d",n);
if(n%100!=11&&n%10==1) printf("st");
else if(n%100!=12&&n%10==2) printf("nd");
else if(n%100!=13&&n%10==3) printf("rd");
else printf("th");
printf(" humble number is %d.\n",h[n]);
}
return 0;
}