#include <stdio.h>#include<stdlib.h>//Number of movesintHanoiintnum) { Static intall =0; if(num = =1) { return 1; } Else{ All= Hanoi (Num-1) *2+1; } returnAll ;}//Move StepsvoidHANOI2 (intNumCharACharBCharC) { if(num = =1) {printf ("%c->%c\n", A,c); return 1; } Else{hanoi2 (num-1, a,c,b); printf ("%c->%c\n", A, C); HANOI2 (Num-1, B,a, C); }}voidMain () {printf ("Please enter the number of Hanoi layers! \ n"); intnum; scanf_s ("%d", &num); printf ("\ n Number of moves =%d\n", Hanoi (num)); printf ("move the method as follows \ n"); HANOI2 (num,'A','B','C'); System ("Pause");}
The pursuit of running speed is best not recursive, the pursuit of simple code of the word with recursion.
Loops can be realized, recursion can also be realized, and recursive can be realized, the loop may not be realized.
Note that execution is performed in order before recursion, and the order of execution after recursion is reversed.
"Hanoi" Hanoi number of moves and move steps