题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1058
http://acm.hdu.edu.cn/showproblem.php?pid=3199
这两道题很类似,其思想就是从最小的满足条件的算起,一步步直到求出结果;
hdu 1058
1 #include<iostream> 2 #include<algorithm> 3 const int N=5843; 4 #define min(a,b) (a)<(b)?(a):(b) 5 using namespace std; 6 int num[N]; 7 8 int main(){ 9 int i=1,j=1,k=1,l=1; 10 num[1]=1; 11 for(int count=2;count<=N;count++){ 12 int a=num[i]*2; 13 int b=num[j]*3; 14 int c=num[k]*5; 15 int d=num[l]*7; 16 int ans=min(a,min(b,min(c,d))); 17 if(a==ans)i++; 18 if(b==ans)j++; 19 if(c==ans)k++; 20 if(d==ans)l++; 21 num[count]=ans; 22 } 23 int n; 24 while(scanf("%d",&n)!=EOF&&n){ 25 printf("The %d",n); 26 if(n%100!=11&&n%10==1)printf("st"); 27 else if(n%100!=12&&n%10==2)printf("nd"); 28 else if(n%100!=13&&n%10==3)printf("rd"); 29 else printf("th"); 30 printf(" humble number is %d.\n",num[n]); 31 } 32 return 0; 33 }
hdu 3199:
1 #include<iostream> 2 #include<cstring> 3 const int N=200000000; 4 #define min(a,b) (a)<(b)?(a):(b) 5 using namespace std; 6 __int64 f[N]; 7 8 int main(){ 9 __int64 p1,p2,p3,n; 10 while(scanf("%I64d%I64d%I64d%I64d",&p1,&p2,&p3,&n)!=EOF){ 11 f[1]=1; 12 __int64 i=1,j=1,k=1; 13 for(int count=2;count<=n+1;count++){ 14 __int64 a=f[i]*p1; 15 __int64 b=f[j]*p2; 16 __int64 c=f[k]*p3; 17 __int64 ans=min(a,min(b,c)); 18 if(ans==a)i++; 19 if(ans==b)j++; 20 if(ans==c)k++; 21 f[count]=ans; 22 } 23 printf("%I64d\n",f[n+1]); 24 } 25 return 0; 26 }