If the pillar is labeled A,b,c, move from A to C, and move it directly to C when there is only one plate; when there are two plates, B is used as the auxiliary column; If the number of disks is more than 2, it is very simple to cover up the second plate, and it is easy to handle two plates at a time, namely: A->b, A- >c, b->c The three steps, and the part that is covered up, in fact, by the recursive processing of the equation.
The code is as follows:
#include <stdio.h>void Hanoi (int n,char A,char B,char C) {if (n = = 1) {printf ("Move sheet%d from%c to%c\n", n,a,c);} Else{hanoi (n-1,a,c,b);p rintf ("Move sheet%d from%c to%c\n", N,a,c); Hanoi (N-1,B,A,C);}} int main () {int n;printf ("Please enter the number of disks:"), scanf ("%d", &n), Hanoi (N, ' A ', ' B ', ' C '); return 0;}
Operation Result:
Copyright NOTICE: This article is iflei original article, reprint please indicate source.
Hanoi Tower of Hanoi