[C language] Take you hand in hand to solve the problem of frog jumping steps

insert image description here

Junxi_'s personal homepage

Be diligent and encourage the years to wait for no one

C/C++ game development


Hello, this is Junxi_. Today’s update is the classic recursive problem - frog jumping steps. Among all the problems related to recursion, frog jumping steps is one of the most well-known problems. It can be said that if you can really understand the problem of frogs jumping steps, your recursion will not be too bad. Today I will take you to analyze the problem of frogs jumping steps in depth.

Detailed description of the problem of frog jumping on stairs

A frog can jump up 1 step or 2 steps at a time. Find how many ways the frog can jump up a n-level step.


Concretely solve the problem of frog jumping steps

Analysis of the Solution to the Problem of Frog Jumping on Stairs

  • When N=1, then the frog has only one jumping method.
    insert image description here

  • When N=2, the frog can jump twice on the first step and once on the second step. There are two jumping methods.
    insert image description here

  • When N=3, the frog can jump one level of steps first, then it needs to jump two steps, then it is the jumping method when N=2 at this time, there are two jumping methods.

  • When the frog jumps two steps once, it only needs to jump one step at this time, then it is the jumping method when N=1.

  • At this time, its jumping method is equal to (N=1)+(N=2) jumping methods.
    insert image description here

  • When N=4, when the frog jumps one step at a time, it still needs to jump three steps, then its remaining jumping method at this time is equal to the jumping method when N=3, that is, there are three jumping methods .

  • When the frog jumps two steps once, there are still two steps left to jump, then the remaining jumping method at this time is the jumping method when N=2, and there are two jumping methods.

  • Then its jumping method at this time is equal to (N=2) + (N=3) jumping methods.
    insert image description here

Idea summary

  • Then, it is not difficult to see the law of frog jumping steps. When N>2, the number of jumps at this time is equal to the sum of the number of jumps of the previous two frogs.

Implementation of Frog Jumping Stairs in C Language

  • Code
#include<stdio.h>
int Jump(int n)
{
    
    
	if (n == 1)
	{
    
    
		return 1;//当只有一层台阶时直接返回1
	}
	 if (n == 2)
	{
    
    
		return 2;//当只有2层台阶时就返回2
	}
	 if (n > 2)
	 {
    
    
		 return Jump(n - 1) + Jump(n - 2);
	 }//当n>2时,利用递归进行返回
}
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	int num = Jump(n);
	printf("%d\n", num);
	return 0;
}
 

Advanced question:

A frog can jump up 1 step, 2 steps, or 3 steps at a time. Find how many ways the frog can jump up a n-level step.

Idea analysis

  • First of all, when N=1, then the frog has a jumping method.

  • When N=2, the frog can jump twice on the first step and once on the second step. There are two jumping methods

  • When N=3, when the frog jumps one step first, then it needs to jump two steps, then it is the jumping method when N=2 at this time, that is, there are two jumping methods.
    When the frog jumps two steps once, it only needs to jump one more step at this time, then it is the jumping method when N=1 at this time, that is, there is a jumping method.

    In the end, the frog jumps directly on three steps, and there is only one way to jump. Then its total number of jumps is (N=1)+(N=2)+1=4 kinds of jumps.

  • When N=4, when the frog jumps one step at a time, there are still three steps left to jump, then it is the jumping method when N=3 at this time, that is, there are four jumping methods.
    When the frog jumps two steps once, there are still two steps left to jump, then it is the jumping method when N=2 at this time, and there are two jumping methods. Then its total number of jumps is (N=2)+(N=3)=6 jumps.

  • Law summary

  • When N>3, it is essentially the sum of the previous two jumping methods to get the total number of frog jumping steps at this time.

  • The code demonstration is as follows:

#include<stdio.h>
int Jump(int n)
{
    
    
	if (n == 1)
	{
    
    
		return 1;
	}
	if (n == 2)
	{
    
    
		return 2;
	}
	if (n == 3)
	{
    
    
		return 3;
	}
	if (n > 3)
	{
    
    
		return Jump(n - 1) + Jump(n - 2)+Jump(n-3);
	}
}
 
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	int num = Jump(n);
	printf("%d\n", num);
	return 0;
}
  • Note:
  • When the frog can jump k steps each time, we can also find the corresponding law to solve it as above

Summarize

  • This is the end of today's content. Today we specifically solved the problem of frog jumping steps through recursion. If you are still confused about the steps, you must try it yourself, otherwise it is very easy to forget and difficult to understand just by looking at it, we have to practice repeatedly to get familiar with it!

  • Well, if you have any questions, please ask me in the comment area or private message, see you next time!

It is not easy for a new blogger to create. If you feel that the content of the article is helpful to you, you may wish to click on this new blogger before leaving. Your support is my motivation to update! ! !

**(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)**

insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/131464294