递归--汉诺塔问题

来源:互联网 发布:网络套现违法吗 编辑:程序博客网 时间:2024/05/16 19:37
#include <iostream>using namespace std;int num=1;void move(char getone,char putone){cout<<getone<<"-->"<<putone<<endl;}void hanoi(int n,char one,char two,char three){void move(char getone,char putone);if (n==1) move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);num+=2;}}int main(){void hanoi(int n,char one,char two,char three);int m;cout<<"Enter the number of diskes:";cin>>m;cout<<"the steps to moving "<<m<<" disk:"<<endl;hanoi(m,'A','B','C');cout<<"total steps is: "<<num<<endl;return 0;}

原创粉丝点击