Hi~ o(* ̄▽ ̄*)ブ我是闪电,今天给大家分享汉诺塔代码(c++):
#include <iostream>
using namespace std;
void tower(int n,char now,char help,char to)
{
if(n==1)
{
cout<<now<<"-->"<<n<<"-->"<<to<<endl;
return ;
}
tower(n-1,now,to,help);
cout<<now<<"-->"<<n<<"-->"<<to<<endl;
tower(n-1,help,now,to);
}
int main()
{
int n;
char a,b,c;
cin>>n>>a>>b>>c;
tower(n,a,b,c);
return 0;
}