16, Toad's data structure notes of 16 stacks of application stack and the recursive question of the Nottingham Tower
This famous article: "The value of life, not by time, but by depth to measure." "
Continuation stack with recursive application, Hanoi Tower problem.
Welcome reprint, reproduced please indicate the source:
1. Hanoi Tower Problem
Hanoi: Hanoi (also known as Hanoi) is a puzzle toy from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.
Regardless of the credibility of the legend, if you consider the 64 pieces of gold, from one needle to the other needle, and always keep the small lower order. How many times does it take to move? This requires a recursive approach. Suppose there are n slices, and the number of moves is f (n). Obviously f (1) =1,f (2) =3,f (3) = 7, and f (k+1) =2*f (k) +1. It is not difficult to prove F (n) =2^n-1 thereafter. When N=64,
How long will it take to do it every second? A common year 365 days has 31.536 million seconds, leap year 366 days have 31.6224 million seconds, average yearly 31,556,952 seconds, calculate:
1.,844,674,407,370,96e,+19 seconds
This suggests that it takes more than 584.554 billion years to finish the gold, and that the earth has been around for only 4.5 billion years, and the life expectancy of the solar system is said to be tens of billions of years. After 584.554 billion years, not to mention the solar system and the Milky Way, at least all the life on Earth, along with the Vatican tower, temples, etc., have already been destroyed by the Ashes.
2. Source Code
Do not enter too large numbers during commissioning, be careful of the Ashes ~
#include "Stdio.h"
void Hanoi (intn,chara,charb,char c)
{
if (n==1)
printf (" move %d disks directly from %c to %c\n",n,a ,c);
Else
{
Hanoi (N-1,a,c,b);
printf ( Span style= "color: #A31515; Background:lime ">"   will %d disks from %c move to %c\n" , n , a , c );
Hanoi (N-1,b,a,c);
}
}
void Main ()
{
int N;
printf (" Please enter the number of plates to be moved \ n");
scanf ("%d", &n);
printf ("***********************************\n");
Hanoi (n,' x ',' y ',' z ');
printf ("***********************************\n");
}
As shown in 1:
3. Recursion Disadvantage
Recursion has an important disadvantage. When a recursive function is used, the data generated by each calculation and the value and return address of the variable are stored, also known as "protected sites," when the recursive process has not been completed, and if the function is carried out in very many times, the system generates very much temporary data, and the system will store the data. Cause a very memory-intensive, data reaches a certain amount of data, may cause the computer to panic. So, the recursive function is good, we have to use it carefully.
In the recursive process, we do not use to understand the internal operation of the computer, because the computer system itself will build a stack to hold the data, until the end of the recursive system will automatically start reading data and return values. When the recursion is all over, the system frees up the stack that holds the temporary data.
Water and electricity
16, Toad's data structure notes of 16 stacks of application stack and the recursive question of the Nottingham Tower