#include<iostream>
#include<stdlib.h>
using namespace std;
void Hanoi(int n,char A,char B,char C){ //n个盘子从A借助B到C上
if(n==1){
cout<<A<<"-->"<<C;
}
if(n==2){
cout<<A<<"-->"<<B<<endl;
cout<<A<<"-->"<<C<<endl;
cout<<B<<"-->"<<C<<endl;
}
else{
Hanoi(n-1,A,C,B); //n-1个盘子从A借助C到B上
cout<<A<<"-->"<<C<<endl;//将第n个直接移到C上
Hanoi(n-1,B,A,C);//最后将n-1个盘子借助A移到C上
}
}
int main()
{
int i;
cout<<"请输入汉诺塔的高度:";
cin>>i;
Hanoi(i,'A','B','C');//目的是将n个盘子从A借助B到C上
system("pause");
return 0;
}
What doesn't kill you makes you stronger,stand a little taller
Doesn't mean I'm lonely when I'm alone.
What doesn't kill you makes a fighter,Footsteps even lighter
Doesn't mean I'm over cause you're gone.
————即便生命枯竭,亦在优雅中变老。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步