Classic recursion (Towers of Hanoi)

_CRT_SECURE_NO_WARNINGS #define
#include <stdio.h>
#include <stdlib.h>
int I =. 1; // record the number of steps
void Move2 (int n, char a , char c) // the number n of the plate is moved by a to C
{
the printf ( "% d step:% d of a plate of% C ->% C \ n-", I ++, n-, A, C);
}
void Move1 is (int n-, char A, char B, char c) // n disks from an initial to a column to a target column C
{
IF (n ==. 1)
{
Move2 comes (. 1, a, C); // only one plate is moved directly from a to C
}
the else
{
Move1 (n - 1, a, c, b); // n-1 will be a plate before the initial object column by a column c is moved to column b the borrowed
Move2 (n, a, c) ; // remaining one plate moves to the destination column C
Move1 is (n--. 1, B, a, C); // Finally movement borrow column b n-1 to the object of a plate column
}
}
int main ()
{
the printf ( "Please enter the number plate");
int n-;
Scanf ( "% D", & n-);
char X = 'A', Y = 'B', Z = 'C';
the printf ( "plate moves as follows: \ n-");
Move1 is (n-, X, Y, Z);
System ( "PAUSE");
0 return;
}

Results are as follows
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/belongHWL/article/details/90217081