汉诺塔

#include<iostream.h>
void move(char source,char target)
{
 cout<<source<<"==>"<<target<<endl;
}
void hanoi(int n,char source,char temp,char target)
{
 if(n==1)
  move(source,target);
 else
 {
  hanoi(n-1,source,target,temp);
  move(source,target);
  hanoi(n-1,temp,source,target);
 }
}
void main()
{
 int n;
 while(1)
 { 
 cout<<"Input the number of disks:"<<endl;
 cin>>n;
 cout<<"The step to moving "<<n<<" disks:"<<endl;
 hanoi(n,'A','B','C');}
}

你可能感兴趣的:(汉诺塔)