UVa新汉诺塔问题(A Different Task,Uva 10795)
主要需要理递归函数计算
1 #define MAXN 60+10 2 3 #include<iostream> 4 using namespace std; 5 6 int n,k,S[MAXN],F[MAXN]; 7 8 long long f(int *P,int i,int final) 9 { 10 if(i==0) return 0; 11 if(P[i]==final) return f(P,i-1,final); 12 return f(P,i-1,6-P[i]-final)+(1LL<<(i-1)); 13 } 14 15 int main() 16 { 17 int kcase=0; 18 while(cin>>n) 19 { 20 if(n==0) break; 21 for(int i=1;i<=n;i++) cin>>S[i]; 22 for(int i=1;i<=n;i++) cin>>F[i]; 23 k=n; 24 while(k>=1&&S[k]==F[k]) k--; 25 if(k>=1) 26 cout<<"Case "<<++kcase<<": "<<f(S,k-1,6-S[k]-F[k])+f(F,k-1,6-S[k]-F[k])+1<<endl; 27 else 28 cout<<"Case "<<++kcase<<": "<<0<<endl; 29 } 30 return 0; 31 }