Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Hanoi Tower Formula in C language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is the Hanoi Tower Formula in C language". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "what is the Hanoi Tower Formula in C language".

The formula of the tower of Hanoi at the mathematical level:

Needless to say, you must look confused when you see this formula. I will now explain the function of this formula.

First, think about how many steps it takes for an elephant to put in the refrigerator, three steps, open the refrigerator, put it in, and close the door. Let's not think about some detailed steps, simplify a complex problem first, and then analyze it slowly.

The Tower of Hanoi problem is the same simple three steps: (suppose there are n plates)

First, leave the largest plate in column A, and then put all the other plates in column B.

Second, put the largest plate on the C column.

Third, then put all the plates on the B-pillar to the C-pillar.

This is the process of the Tower of Hanoi. The essence of the Tower of Hanoi is the above three sentences.

The tower of Hanoi on the n floor has (2 ^ n-1) moves to move all the plates from disk A to disk C.

C language recursive formula

Accordingly, we can write the corresponding C language recursive formula: (n is the number of plates, xyz is the name of the column)

I'm sure you have a lot of questions. Let's give a few examples before explaining the problem.

Let's not talk about a plate, because he is the biggest plate, so he went directly to disk C.

Two-story Hanoi Tower

A total of three steps: put all the top of the largest disk to B, then the maximum disk to C, and then put all the rest of the disk to C.

These are two disks. they will be moved three times in all. What about the three discs?

Three-story Hanoi Tower

The whole process can be regarded as a whole, and all the disks on the largest disk can be regarded as a whole, and we only need to carry out three steps. We should make use of the point of view of reducing major matters to small ones, and we should not think about how to move in the first place. So you can't see the nature of the problem.

Let's analyze in detail how to move in three steps.

In the first step, we have to move three times, which is A-> C, A-> B, C-> B, which is a large complete movement. In this step, we apply the previous Hanoi Tower formula to use, which is the difficulty of the Hanoi Tower. Next, I'll show you a picture, hoping you can understand. (n is the number of layers, Xphay Y Z. is the function parameter.)

The interior of the Tower of Hanoi is actually like a pyramid. In fact, every time you call yourself, you call yourself according to the formula of the essence mentioned above, so that your parameters have changed. I hope you can follow the drawing process by yourself.

Step 2: move A to C, which is the second step in the image above and write the fourth move: a-> C.

The third step is to put all the plates on the B column to C with the help of A.

After the seventh step is completed, you will find that there are no statements to be executed, the Hanoi function ends and returns to the main function, and the step of solving the Hanoi function is completed.

Okay, in this way, the process of moving the three-story tower of Hanoi is completed, and the completion of the tower of Hanoi three times can solve this problem, because even if there are more plates, it is just the same formula. Understand the operation principle of the two-story and three-story Hanoi tower, and no matter how the tower is multi-storey, it is the same process. It is not difficult to find that recursion is to let the mathematical formula be reflected in C language, so that the problem becomes very "simple".

The rest is the main function part of the program, the main function of this problem is very simple, the main function only uses the number of plates and the names of the three columns; the code is as follows

# include void change (char xjingchar y) / / function {printf ("% c->% c\ n", x, y);} void f (n, x, y) / / Hanoi tower function {if (n = = 1) {change (x, z);} else {f (n-1, x, z, y) / / Formula 1: move the plates outside the A column to the B column change (x, z) with the help of C column; / / Formula 2: move the maximum plate on A to the C column f (n-1, y, x, z); / / Formula 3: put all the plates on the B column to the C column} int main () {int m; scanf ("% d", & m) with the help of A. F (m,'A','B','C');} above is all the contents of the article "what is the Hanoi Tower Formula in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report