In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use recursion in C language to solve the problem of frog jumping steps, which is very detailed and has a certain reference value. Interested friends must finish reading it!
First, the train of thought of solving
The number of steps is n.
When n = 1, frogs have a way to jump, that is, to jump one step.
When n = 2, frogs have two ways of jumping, that is, jumping one step twice or jumping two steps once.
When n = 3, the frog can jump two steps and then one step, or choose to jump one step first and then two steps, or jump one step three times. By analogy, we can know how frogs jump when the number of steps is n.
However, is this very troublesome? think about it again.
Or when n = 3, we choose to jump one step first, and whether the jump of the remaining two steps is the frog's jump when n = 2; we choose to jump two steps first, and the remaining one is the frog's jump when n = 1.
It can be seen that the jumping method of frogs when n = 3 is the jumping method of n = 1 plus the jumping method of n = 2.
When n = N, the jump method of N steps is the jump method of NMUI 1 and the jump method of NMUI 2.
At first glance, does it feel a bit like the Fibonacci sequence? of course, there is still something different, but we can use the same mathematical idea to solve the problem.
Second, the code implementation 1. Reference code # define _ CRT_SECURE_NO_WARNINGS 1#include int flog (int n) {if (n = 1) return 1; else if (n = 2) return 2; else return flog (n-1) + flog (n-2);} int main () {int n = 0; int ways = 0 Printf ("Please enter the number of steps:"); scanf ("% d", & n); ways = flog (n); printf ("frogs have% d jumps", ways); return 0;} 2. Running result
These are all the contents of the article "how to use Recursion to solve the problem of Frog jumping steps in C language". Thank you for reading! Hope to share the content to help you, more related 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.