HDOJ:1058
第一种思路:
对于四个因数:2,3,5,7;首先定义a[original]=1,m=n=x=y=1=original;然后再分别用a[m],a[n],a[x],a[y],来表示2,,3,5,7的个数再比较a[m]乘以2,a[n]乘以3,a[x]乘以5,a[y]乘以7的大小;如果小的那个是a[x],则x++,并将此数值有a[++original]记录下来,再进行比较四者的大小,不断重复直到original==5843;
第一种思路的代码(AC了,要注意输出的格式,我在这里错了4次):
#include <iostream> #include <algorithm> #define Max 5844 using namespace std; long long int a[Max]; void hanshu() { int orginal=1,m=1,n=1,x=1,y=1; a[orginal]=1; long long int temp,temp1,temp2; temp=a[orginal]; while(orginal!=5843) { temp1=min(a[n]*2,a[m]*3); temp2=min(a[x]*5,a[y]*7); temp=min(temp1,temp2); if(temp==a[n]*2) n++; else if(temp==a[m]*3) m++; else if(temp==a[x]*5) x++; else if(temp==a[y]*7) y++; if(temp!=a[orginal]) a[++orginal]=temp; } } int main(void) { freopen("in.txt","r",stdin); hanshu(); int n; while(cin>>n&&n) { if(n%10==1&&n%100!=11) cout<<"The "<<n<<"st humble number is "<<a[n]<<"."<<endl; else if(n%10==2&&n%100!=12) cout<<"The "<<n<<"nd humble number is "<<a[n]<<"."<<endl; else if(n%10==3&&n%100!=13) cout<<"The "<<n<<"rd humble number is "<<a[n]<<"."<<endl; else cout<<"The "<<n<<"th humble number is "<<a[n]<<"."<<endl; } fclose(stdin); return 0; }