Home > Back-end >  Hanoi (recursive)
Hanoi (recursive)

Time:10-15

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;
}

CodePudding user response:

refer to the second floor qybao 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;
}

Don't mean that

CodePudding user response:

reference m0_50813063 reply: 3/f
don't mean that

How to don't mean that? What dish serial number and size requirement? Serial number number when the smaller the dish size, the greater the number is not good? If order, it is likely big plate pressure on the small plate, is it?

CodePudding user response:

reference 4 floor qybao response:
Quote: reference m0_50813063 reply: 3/f

Don't mean that

How to don't mean that? What dish serial number and size requirement? Serial number number when the smaller the dish size, the greater the number is not good? If order, it is likely big plate pressure on the small plate, is it?

It has nothing to do with the plate size, that is, each serial number of the tower is from down to up

CodePudding user response:

reference 5 floor m0_50813063 reply:
it has nothing to do with the plate size, is that each serial number of the tower is from down to up increase

Look at the pictures you send, be want such result, is the control of printing serial number, recursive itself did not change)
The plate number increases gradually from bottom to top
//parameter description: x, y, z pillars, num plate (with Numbers), len dish array length, CNT pole number plate, step step 
Void Hanoi (char x, char y, char z, int * num, int len, int CNT, int * step) {
If (CNT==1) {
Printf (" % d step: move % d, % c - & gt; Step % c \ n ", * and num [1] CNT, x, z);
(* step) + +;
return;
}
Hanoi (x, z, y, num, len, CNT - 1, step);
Printf (" % d step: move % d, % c - & gt; Step % c \ n ", * and num [1] CNT, x, z);
(* step) + +;
Hanoi (y, x, z, num, len, CNT - 1, step);
}

Int main () {
Int num={5,4,3,2,1} [], len=5, CNT=5, step=1;//num array is the plate number, from top to bottom decreases (increases gradually from bottom to top)
Char x=x, y='y', z='z';
Hanoi (x, y, z, num, len, CNT, & amp; Step);
return 0;
}

CodePudding user response:

To put it bluntly
Hanoi (char one, char tow, char three, int n) this part of the parameters in the same
Hanoi (char x, char y, char z, int int len , int CNT, I nt * step
More than just the num, len and step to control the number of print and the serial number plate, so it is the original han tata's train of thought, only a few more print control

CodePudding user response:

refer to 6th floor qybao response:
Quote: refer to the fifth floor m0_50813063 reply:

It has nothing to do with the plate size, that is, each serial number of the tower is from down to up increase

Look at the pictures you send, be want such result, is the control of printing serial number, recursive itself did not change)
The plate number increases gradually from bottom to top
//parameter description: x, y, z pillars, num plate (with Numbers), len dish array length, CNT pole number plate, step step 
Void Hanoi (char x, char y, char z, int * num, int len, int CNT, int * step) {
If (CNT==1) {
Printf (" % d step: move % d, % c - & gt; Step % c \ n ", * and num [1] CNT, x, z);
(* step) + +;
return;
}
Hanoi (x, z, y, num, len, CNT - 1, step);
Printf (" % d step: move % d, % c - & gt; Step % c \ n ", * and num [1] CNT, x, z);
(* step) + +;
Hanoi (y, x, z, num, len, CNT - 1, step);
}

Int main () {
Int num={5,4,3,2,1} [], len=5, CNT=5, step=1;//num array is the plate number, from top to bottom decreases (increases gradually from bottom to top)
Char x=x, y='y', z='z';
Hanoi (x, y, z, num, len, CNT, & amp; Step);
return 0;
}

Thank you, bosses,

CodePudding user response:

refer to 6th floor qybao response:
Quote: refer to the fifth floor m0_50813063 reply:

It has nothing to do with the plate size, that is, each serial number of the tower is from down to up increase

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related