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

Classic case of C language how to realize the problem of Frog jumping steps and Tower of Hanoi

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

Share

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

This article mainly introduces "how to realize the classic case of frog jumping step and Hanoi tower in C language". In daily operation, I believe that many people have doubts about how to realize the problem of frog jumping step and Hanoi tower in C language classic case. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to realize the classic case of frog jumping steps and Hanoi Tower in C language"! Next, please follow the editor to study!

Catalogue

Frogs jump over the steps

Title

Train of thought

Analysis.

1. Analysis from the number of jumps

two。 From process analysis

II. Frog step jump variant 1

Title

Analysis.

Third, frog jump step variant 2

Title

Analysis.

Fourth, the problem of Hanoi Tower (number of steps)

Title

Train of thought

Analysis.

Fifth, the problem of the Tower of Hanoi (seeking the moving process)

Title

Train of thought

Analysis.

First, the problem of frogs jumping steps

A frog can jump up one or two steps at a time. How many ways can the frog jump up an n-step?

Train of thought

When we encounter a problem, we can first draw on the paper, list the simplest ways, make a comparison, and find the rules.

Analysis.

According to the above table, you can find the rules from the number of jumps, the process, or a combination of both.

1. Analysis from the number of jumps

Looking at the table, we can see that from n > = 3, the nth number is the sum of the first two numbers (the same as the Fibonacci series).

We infer that when the number of steps is n, let the jump method have f (n) times, if the frog jumps 1 step first, then the remaining step number is nmur1, that is, if the frog jumps 2 steps first, the remaining step number is 2 times, that is, if the frog jumps 2 times, the remaining step number is 2 times.

Therefore, the number of jump times f (n) = f (nmur1) + f (nmur2), because there are two values on the right side of the equal sign, it is the last special restriction when nasty 1 is naughty 2.

The following code is recursive. If you want to use non-recursion, you can change the recursive general term to a loop.

Code 1 (Recursive)

# include int jump (int n) {if (n = 1) return 1; if (n = 2) return 2; return jump (n-1) + jump (n-2);} int main () {int n; scanf ("% d", & n); int ret = jump (n); printf ("% d", ret); return 0;} 2. From process analysis

If you look at the table, you can know that jumping n steps and jumping two steps can range from 0 to 2 times, and the order of jumping two steps each time is also uncertain. The combinatorial number C (n _ (m)) of the counting principle can be used to represent the arrangement of m numbers selected from n. N represents the number of jumps each time, and m represents the number of times to jump two steps at a time.

The combinatorial number C (n _ (m)) can be determined by n! / (m! * (nMurm)!) Obtain

The following code is a non-recursive method. If you want to write it recursively, you can modify it according to the loop.

Code 2 (non-recursive)

# include int fac (int m) {int I = 0; int count = 1; for (I = 1; I B, second B-> A, back and forth) printf ("% c->% c\ n", x, z); / / print the process of moving the initial column to the target column hanio (n-1, y, x, z) / / put a plate from the starting column to the target column (the first B-> C, the second C-> B, back and forth)}} int main () {int n; scanf ("% d", & n); hanio ("% d", & n); hanio; return 0;} this is the end of the study on "how to realize the frog jump steps and the Hanoi Tower problem in the classic case of C language". Hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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