Hanoi rule: Have n plate, from the first pillar, moved to the third pillar, requires each dish down from pillar to post, the serial number is growing up,
CodePudding user response:
"Given a bit of input, a complete single-step tracking (and press Alt + 7 key to view the Call Stack from the inside to the following out of from the inner to outer function Call history) again," is your least-hassle route to understand the working principle of the recursive function! Recursive function pay attention to the following factors Exit criteria, What parameters, What, the return value is What are local variables, What are global variables, When output, , will not lead to stack overflow
CodePudding user response:
This is not the most classic recursion? First with the help of the third pillar of the first column n - 1 dishes have been moved to the second pillar, and then put the first pillar of the NTH plate has to the third pillar; Then with the help of the first pillar of the second pillar of n - 1 plate moved to the third pillar, Char
void Hanoi (char one, tow, char three, int n) { If (n==1) { Printf (" from the first the first % d % c pillars moving plate to the % c pillar \ n ", one, n, three); return; } //printf (" using the % c a pillar to the % % d c a a pillar, a plate to move to the first % c pillar \ n ", three, one, n - 1, tow); Hanoi (one, three, tow, n - 1); Printf (" from the first the first % d % c pillars moving plate to the % c pillar \ n ", one, n, three); //printf (" using the % c a pillar to the % % d c a a pillar, a plate to move to the first % c pillar \ n ", one, tow, n - 1, three); Hanoi (tow one, three, n - 1); }
Int main () { Int n=5; Char a='1', b='2', c='3'. Hanoi (a, b, c, n); return 0; }