今天编写了一下Hanoi汉诺塔 对递归有了更深的理解
//Hanoi汉诺塔 问题
#include<iostream>
using namespace std;
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;
}
int main()
{
int m;
cout<<"input the number of dishs:";
cin>>m;
cout<<"the steps of moveing"<<m<<"disks:"<<endl;
hanoi(m,'A','B','C');
return 0;
}