老刘动归PPT里最后一题 里面已给出算法提示
注意输出时需要遵循的后缀原则
#include<iostream>
#include<string>
using namespace std;
int main(){
int i,n,x2,x3,x5,x7,f[5843]={0,1};
string r;
for(x2=x3=x5=x7=1,i=2;i<5843;++i){
f[i]=min(min(f[x2]*2,f[x3]*3),min(f[x5]*5,f[x7]*7));
if(f[i]==f[x2]*2)x2++;//全用if而不用else if 防止出现重复
if(f[i]==f[x3]*3)x3++;
if(f[i]==f[x5]*5)x5++;
if(f[i]==f[x7]*7)x7++;
}
while(cin>>n,n){
if(n%10==1&&n%100!=11)r="st";
else if(n%10==2&&n%100!=12)r="nd";
else if(n%10==3&&n%100!=13)r="rd";
else r="th";
cout<<"The "<<n<<r<<" humble number is "<<f[n]<<"."<<endl;
}
}