Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Recursion to solve the problem of Frog jumping steps in C language

Shulou Source: shulou.com Published: 2022-06-02 12:51:58 10月02日 Update

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!

Tags: Steps frogs questions choices language recursion code content that is quantity articles or references from which we can know value interest boys buddies ideas ideas. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Microsoft Docker Huawei