汉诺塔

来源:互联网 发布:俄克拉何马爆炸案 知乎 编辑:程序博客网 时间:2024/03/29 20:03
#include<iostream>using namespace std;int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"请输入需要移动盘子的个数:";cin>>m;cout<<"移动"<<m<<"个盘子的步骤是:"<<endl;hanoi(m,'A','B','C');return 0;}void hanoi(int n,char one,char two,char three)        //将n个盘子从one座借助two座移到three座{void move(char x,char y);if (n==1)move(one, three);else {hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);}}void move(char x,char y){cout<<x<<"--->"<<y<<endl;}

原创粉丝点击