hdu1058Humble Numbers
2 3 5 7的倍数,其中一个数的2 3 5 7倍也是同类型数,根据这个来递推,每个数都有机会乘于2,3,5,7,乘完进行比较,——这种方法是从前面推出后面的数据,不像之前解过的从后面推到前面,所以想不出来,主要是看到数据类型的题意,我就感觉做不了了,看来以后不可以放弃的太快
#include "iostream" using namespace std; int min(int a,int b){return a>b?b:a;} int main(){ int f[5850],i,i1=1,i2=1,i3=1,i4=1,n; f[1]=1; for(i=2;i<=5842;i++){ f[i]=min(f[i1]*2,min(f[i2]*3,min(f[i3]*5,f[i4]*7))); if(f[i]==f[i1]*2){i1++;} if(f[i]==f[i2]*3){i2++;} if(f[i]==f[i3]*5){i3++;} if(f[i]==f[i4]*7){i4++;} } //for(i=1;i<100;i++)cout<<i<<' '<<f[i]<<endl; while(cin>>n&&n){ 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; } }