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 place the discs from the following beginning in order of size and again on a pillar. It is also stipulated that the disc cannot be enlarged on a 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 was dressed in a needle from the bottom to the top of the big-to-small sheet of Gold, the so-called 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 a needle to a needle, and always kept in a small order. How many times does this need to move??A recursive approach is required here. If 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 suggests that the removal of these pieces of gold requires 5845.54 more than billion years, and the Earth exists so far just $ 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 execution are as follows:
The object-oriented approach solves this problem:
#include <iostream>class han{private:/* plate number */int num;public:void Hanoi (int n, char A, Char B, char C) {static int num = 1;num++;if (n = = 1) {std::cout << "move plate << N <<" from "<< A <<" to "<< C << St D::endl;return;} else {Hanoi (n-1, A, C, B) std::cout << "Move plate << N <<" from "<< A <<" to "<< C <&L T Std::endl;hanoi (n-1, B, A, C);}} /*getter setter*/void setnum (int num) {this->num = num;} int Getnum () {return this->num;}}; int main (int argc,char *argv[]) {int num;std::cout << "Please enter the number of plates:" << std::endl;std::cin >> num;std:: cout << "Number of input plates:" << num << std::endl;han hanoi;hanoi.setnum (num) Hanoi.hanoi (Hanoi.getnum (), ' A ', ' B ', ' C '); Std::cin.get (); Std::cin.get ();}
Execution effect:
4. The result extrapolation step demonstrates:
About Hanoi, C + + code, code effect calculus