您的位置:首页 > 其它

汉诺塔问题的递归实现

2014-09-26 11:07 204 查看
#include<iostream>

using namespace std;

int main()

{

void hanoi(int n,char one,char two,char three);

int num;

cout<<"请输入要计算的盘子数:";

cin>>num;

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

}

void hanoi(int n,char one,char two,char 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: