/* *copyright (c) 2016, Yantai University Computer College
*all rights reserved.
* File name: Test2.cpp
*: Liu Jinshi
* Completion Date: March 19, 2016
* version number: v1.0
* Problem Description: Hanoi Tower Problem
* Enter Description: Enter the number of plates you want to move.
* Output Description: Output move step.
* *
#include <iostream>
using namespace std;
void Hanoi (int n,char Src,char medium,char dest);
void Move (char Src,char dest);
int main ()
{
int m;
cout<< "Enter The number of Diskes:" <<endl;
cin>>m;
cout<< "The Step to Moving" <<m<< "diskes:" <<endl;
Hanoi (M, ' A ', ' B ', ' C ');
return 0;
}
void Hanoi (int n,char Src,char Medium,char dest)
{
if (n==1) move
(src,dest);
else
{
Hanoi (N-1,src,dest,medium);//n-1 plate from SRC to Medium, with the help of Dest, move
(src,dest);
Hanoi (N-1,medium,src,dest), and/or n-1 tray from medium to dest, with the help of Src
}
}
void Move (char Src,char dest)
{
cout<<src<< "-->" <<dest<<endl;
Run Result:
Programming Experience:
<span style= "White-space:pre" > </span> through this Hanoi tower problem, so that their deeper understanding of the function recursion, as well as the use of recursion to solve complex problems. Take the subject, to move n plates to the dest, first of all to the top n-1 plate moved to the medium, and then the last plate from SRC directly moved to dest up. But the n-1 plate from SRC moved to the medium obviously can not be completed in one step, to the first n-2 plate moved to dest, and then the SRC on the n-1 plate to move to the medium ... And so on. Finally, move the largest plate to the dest and then move the n-1 plate to the dest, and so on.