#include<iostream>
using namespace std;
int s[5900];
int min(int x,int y,int z,int t)
{
int m;
m=x;
if(y<m) m=y;
if(z<m) m=z;
if(t<m) m=t;
return m;
}
int num()
{
int i,j;
int a,b,c,d;
a=b=c=d=1; // 2,3,5,7的计数器
s[a]=s[b]=s[c]=s[d]=1;
for(i=2;i<5843;i++)
{
s[i]=min(s[a]*2,s[b]*3,s[c]*5,s[d]*7); //规律:取最小值,相应的计数器加1
if(s[i]==2*s[a]) a++;
if(s[i]==3*s[b]) b++;
if(s[i]==5*s[c]) c++;
if(s[i]==7*s[d]) d++;
}
return 0;
}
int main()
{
int n;
num();
while(cin>>n&&n)
{
if(n%10==1&&n%100!=11) cout<<"The "<<n<<"st humble number is "<<s[n]<<"."<<endl;
else if(n%10==2&&n%100!=12) cout<<"The "<<n<<"nd humble number is "<<s[n]<<"."<<endl;
else if(n%10==3&&n%100!=13) cout<<"The "<<n<<"rd humble number is "<<s[n]<<"."<<endl;
else cout<<"The "<<n<<"th humble number is "<<s[n]<<"."<<endl;
}
return 0;
}
C++--ACM之杭电OJ--1058丑数问题
最新推荐文章于 2022-10-23 10:54:59 发布