1 . About the story
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 the 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.
2. Origin
French mathematician Edward Lucas has written an ancient Indian legend: in the world center of Sheng Miao (in northern India), a yellow copper plate is inserted with three stone needles. In the creation of the world, The Hindu Lord Brahma, on one of the needles, dressed from the bottom to the top of the big-to-small sheet of gold, which is called the Hanoi. No matter the day or night, there is always a monk in accordance with the following rules to move these gold pieces: one at a time, no matter where the needle, the small pieces must be on top of the large. The monks predicted that the world would be wiped out in a thunderbolt when all the pieces of gold were moved from the needle on which Brahma was dressed, and that the Vatican, the temples and all sentient beings would perish. [1]
no matter how credible the legend is, consider -a piece of gold, moved from one needle to another, and always kept in a small order. How many times does it take to move??recursive methods are required here. Suppose there isNThe number of moves isf (n).apparentlyf (1) =1,f (2) =3,f (3) =7, andf (k+1) =2*f (k) +1. It's not hard to prove it afterwardsf (n) =2^n-1. n=64when the
How long will it take to do it every second? A common year 365 Day has 31536000 seconds, leap year 366 days have 31622400 seconds, average per year 31556952 seconds to calculate: 18446744073709551615 seconds
This indicates that the removal of these pieces of gold requires 5845.54 over billions of years, and the Earth exists so far but $ for billions of years, the life expectancy of the solar system is said to be tens of billions of years. Really after 5845.54 billion years, not to say that the solar system and the Milky Way, at least all life on Earth, along with the Vatican tower, temples, etc., have already been destroyed by the Ashes.
3. Hanoi's Code
#include <iostream>
voi D han (int n, char A, Char B, char C)
{
static int num = 1;
//std::cout << "section" << num << "Times";
num++;
if (n = = 1)
{
std::cout << "Place plate" << n << "from" << A << "Move to" << C << Std::endl;
return;
}
else
{
han (n-1, A, C, B);
//std::cout << A << "→" << C << Std::endl;
std::cout << "Move plate << N <<" from "<< A <<" to "<< C << Std::endl ;
han (n-1, B, A, C);
}
}
F (n) =2*f (n-1) +1//f (n) =2^n-1
2^64-1
void Main ()
{
int n;
Std::cin >> N;
Std::cout << "n=" << n << Std::endl;
Han (n, ' A ', ' B ', ' C ');
Std::cin.get ();
Std::cin.get ();
}
The results of the operation are as follows:
4. The result extrapolation step demonstrates: