汉诺塔问题的递归实现
栏目:互联网时间:2014-10-08 15:20:59
#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;
}
------分隔线----------------------------
------分隔线----------------------------