这是一道比较简单的动态规划。。。
2 3 4 5 6 7 8 9 10 12 14 15 16
2 3 2*2 5 2*3 7 2*2*2 3*3 2*5 2*2*3 2*7 3*5 2*2*2*2
2=min(1*2, 1*3, 1*5, 1*7)
3=min(2*2, 1*3, 1*5, 1*7)
4=min(2*2, 2*3, 1*5, 1*7)
5=min(3*2, 2*3, 1*5, 1*7)
6=min(3*2, 2*3, 2*5, 1*7)
7=min(4*2, 2*3, 2*5, 1*7)
8=min(4*2, 3*3, 2*5, 2*7)
9=min(5*2, 3*3, 2*5, 2*7)
Write a program to find and print the nth element in this sequence
1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
#include <iostream>
using namespace std;
int min(int a,int b,int c,int d)
{
int min1,min2;
min1=a<b?a:b;
min2=c<d?c:d;
return min1<min2?min1:min2;
}
int main()
{
int f[5845], a, b, c, d, n, i;
f[1] = 1;
a = b = c = d = 1;
for(i = 2; i <= 5842; i++)
{
f[i]=min(f[a]*2, f[b]*3, f[c]*5, f[d]*7);
if(f[i]==f[a]*2)
a++;
if(f[i]==f[b]*3)
b++;
if(f[i]==f[c]*5)
c++;
if(f[i]==f[d]*7)
d++;
}
while(cin>>n,n)
{
// cout<<f[n]<<endl;
if(n%10==1&&n%100!=11)
cout<<"The "<<n<<"st humble number is "<<f[n]<<"."<<endl;
else if(n%10==2&&n%100!=12)
cout<<"The "<<n<<"nd humble number is "<<f[n]<<"."<<endl;
else if(n%10==3&&n%100!=13)
cout<<"The "<<n<<"rd humble number is "<<f[n]<<"."<<endl;
else
cout<<"The "<<n<<"th humble number is "<<f[n]<<"."<<endl;
}
return 0;
}
/*if(n%10==1&&n%100!=11) 加st
else if(n%10==2&&n%100!=12) 加nd
else if(n%10==3&&n%100!=13) 加rd
else 加th*/