汉诺塔问题——递归问题

来源:互联网 发布:mac原生ntfs 编辑:程序博客网 时间:2024/06/09 16:25

#include<iostream>

usingnamespace std;

intmain()

{voidhanoi(int n,char one,char two,char three);

intm;

cout<<"inputthe number of diskes:";

cin>>m;

cout<<"Thesteps of moving "<<m<<" disks:"<<endl;

hanoi(m,'A','B','C');

return0;

}

 

voidhanoi(int n,char one,char two,char three)

//将n个盘从one座借助two座,移到three座

{voidmove(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);

}

}

 

voidmove(char x,char y)

{cout<<x<<"-->"<<y<<endl;}


0 0
原创粉丝点击