提交时间:2024-11-07 16:52:19

运行 ID: 55443

#include<bits/stdc++.h> using namespace std; int cnt=0; void hannoi(int n,char a,char b,char c){ if(n==1){ cout<<a<<"->"<<c<<endl; cnt++; return; } else{ hannoi(n-1,a,c,b); cout<<a<<"->"<<c<<endl; cnt++; hannoi(n-1,b,a,c); return; } } int main(){ int n; cin>>n; hannoi(n,'A','B','C'); cout<<cnt; return 0; }