In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.