Explain the recursive problem of frog jumping

Explanation and Extension of the Recursive Problem of Frog Jumping

Description

A frog can jump 1 step at a time or 2 steps at a time. For example, there is only one way to jump on the first step: just jump 1 step. There are two ways to jump on two steps: one step at a time and two steps at a time; or two steps at a time. How many ways to jump to the nth step?

Problem solving ideas

We set the number of steps N ;
when N=1 , of course, there is only one jumping method;
when N=2 , the frog can jump twice for 1 layer and once for 2 layers;
when N=3 , when there are 3 steps When the frog chooses to jump 1 level first, and 2 levels of steps remain, so at this time, it is the jumping method when there are 2 levels of steps. There are two ways; when the frog chooses to jump 2 levels of steps for the first time, 1 level of steps remains At this time, there is a jump method when there is one step, so the method when there are three steps is: the method of 2 steps + the method of 1 step.
When N=4 , the specific jumping method is: 1. Jump 1 layer first. If you jump 1 layer first, there are 3 layers left, and the next is the jump method of 3 steps. 2. Jump 2 levels first. If you jump 2 levels first, you will be left with 2 levels. The next two are the two-level jump method, so the four-level step method is: the 3-level step method + the 2-level step method.

By analogy, when N=n, the method of n steps is: the method of n-1 steps + the method of n-2 steps .

#include<stdio.h>
int frog(int n)
{
    
    
   if(n == 1)
   {
    
    
      return 1;
   }
   if(n == 2)
   {
    
    
      return 2;
   }
   return frog(n-1) + frog(n-2);
}
int main()
{
    
    
   int n;
   scanf("%d",&n);
   int ways = frog(n);
   printf("%d\n",ways);
   return 0;
}

Extension of the problem

Next, we make a small change to the rules of the game. We allow the frog to jump multiple steps (greater than 2) at a time. What happens to this recursion problem?

First, we let the frog jump 3 steps
at a time . When N=1 , there is 1 method;
when N=2 , there are 2 methods;
when N=3 , the frog can choose the first step to jump 1 step or 2 steps or 3 steps, there are (1, 1, 1), (1, 2), (2, 1), (3) four methods;
when N=4 , the frog chooses the first step to jump 1 layer When the
frog chooses to jump 2 levels in the first step, the remaining 2 steps are the method when n=2; when the frog chooses to jump 3 levels in the first step, The remaining step is the method when n=1; so all methods when n=4 are: all methods in n=3 + all methods in n=2 + all methods in n=1;
and so on, when When N=n, the method of n-layer steps is: n-1 layer method + n-2 layer method + n-3 method;

#include<stdio.h>
int frog(int n)
{
    
    
   if(n == 1)
   {
    
    
      return 1;
   }
   if(n == 2)
   {
    
    
      return 2;
   }
   if(n==3)
   {
    
    
      return 4;
   }
   return frog(n-1) + frog(n-2) + frog(n-3);
}
int main()
{
    
    
   int n;
   scanf("%d",&n);
   int ways = frog(n);
   printf("%d\n",ways);
   return 0;
}

Let's continue to extend the proposal, what if the frog can jump k steps at a time?
Obviously, after the above discussion, this problem has become less complicated. We only need to calculate how many ways there are for the frog to jump 1 level of steps until the frog jumps to k levels. Then, using recursion, it will be easy got the answer.

int frog(int n, int k)
{
    
    
   if(n == 1)
   {
    
    
      return 1;
   }
   if(n == 2)
   {
    
    
      return 2;
   }
   .......
   .......
   if(n == k)
   {
    
    
      return ?//跳 k 层台阶时的方法
   }
   return frog(n-1) + frog(n-2)+ ........+frog(n-k);
}

Guess you like

Origin blog.csdn.net/weixin_52606524/article/details/113037147