当前位置:网站首页>Recursion frog jumping steps problem
Recursion frog jumping steps problem
2022-06-30 02:30:00 【A faint branch】
Catalog
Caption ( Preface )
One 、 Ideas
Two 、 Code
3、 ... and 、 expand
summary

Preface :
A frog can jump at a time 1 A step or jump 2 Stepped steps , for example : There is only one way to jump up the first step : Jump directly 1 Level 2 . Jump up two steps , There are two ways to jump : Each jump 1 level , Jump twice ; Or a jump 2 level . Ask to jump to the n How many jumps are there on the steps ?
One 、 Ideas :
Recursion usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem , The recursion strategy only needs a few programs to describe the repeated calculation needed in the process of solving problems , Greatly reduces the amount of code in the program ;
According to the idea of recursion, we might as well set the number of steps as N
1. When N=1 when , There is a way for frogs to jump up the steps ;
2. When N=2 when , The frog jumped up the steps , You can skip two levels at a time or one level twice , There are two methods
3. When N=3 when , Suppose the number of steps the frog jumps on for the first time is 1, There are two jumping methods for the remaining two steps ; If the number of steps jumped for the first time is 2, There is only one jumping method for the remaining step ; To make a long story short , Namely N=1 The number of hours +N=2 The number of hours .
4. When N=4 when , Suppose the first step the frog jumps on is 1, be left over 3 A stair , That is, the method of jumping three steps ; If you jump two steps for the first time , There are two steps left , That is, the method of jumping two steps
To sum up, we can deduce the law : When the number of steps is N when , The frog jumps on the steps :(N-1) Methods +(N-2) Methods
Two 、 Code :
#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;
}
3、 ... and 、 expand
If a frog can jump more than one step at a time ( Greater than 2), Then jump at this time N How many jumping methods are there for steps ; Also follow the above steps to analyze
1. When N=1 when , There is a way for frogs to jump up the steps ;
2. When N=2 when , The frog jumped up the steps , You can skip two levels at a time or one level twice , There are two methods
3. When N=3 when , Suppose the number of steps the frog jumps on for the first time is 1, There are two jumping methods for the remaining two steps ; If the number of steps jumped for the first time is 2, There is only one jumping method for the remaining step ; Or just jump once 3 A stair .
4. When N=4 when , Suppose the first step the frog jumps on is 1, be left over 3 A stair , That is, the method of jumping three steps ; If you jump two steps for the first time , There are two steps left , That is, the method of jumping two steps ; If you jump three times for the first time , There is a step left , That is the way to jump a step .
To sum up, we can deduce the law : When the number of steps is N when , The frog jumps on the steps :(N-1) Methods +(N-2) Methods +(N-3) Methods
#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;
}summary : Empathy , If a frog can jump at once n A stair , It can also be analyzed according to the above ideas .
边栏推荐
- DDoS "fire drill" service urges companies to prepare
- Precautions for purchasing wildcard SSL certificate
- Bucket sort
- Merge sort
- 2022护网行动在即,关于护网的那些事儿
- 堆排序
- 什么是自签名证书?自签名SSL证书的优缺点?
- AutoJS代碼能加密嗎?YES,AutoJS加密技巧展示
- DHU programming exercise
- Global and Chinese markets for wireless security in LTE networks 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Pytoch learning (II)

【干货分享】最新WHQL徽标认证申请流程

Detailed explanation of minimum stack

Simple distinction between break and continue

打造創客教育中精湛技藝

Five cheapest wildcard SSL certificate brands

Matlab code running tutorial (how to run the downloaded code)

归并排序

What is certificate transparency CT? How to query CT logs certificate logs?
![[dry goods sharing] the latest WHQL logo certification application process](/img/c3/37277572c70b0af944e594f0965a6c.png)
[dry goods sharing] the latest WHQL logo certification application process
随机推荐
Global and Chinese markets for wireless security in LTE networks 2022-2028: Research Report on technology, participants, trends, market size and share
What files does a CA digital certificate contain? How to view SSL certificate information?
选择排序
vs实现快速替换功能
Global and Chinese markets of liquid optical waveguides 2022-2028: Research Report on technology, participants, trends, market size and share
Ffmpeg source code
Global and Chinese market of subscription revenue management software 2022-2028: Research Report on technology, participants, trends, market size and share
Le Code autojs peut - il être chiffré? Oui, présentation des techniques de chiffrement autojs
PEM_ read_ bio_ Privatekey() returns null only in ECB mode - PEM_ read_ bio_ PrivateKey() returns NULL in ECB mode only
重看《Redis设计与实现》后记录几个要点
DDoS threat situation gets worse
2. < tag dynamic programming and 0-1 knapsack problem > lt.416 Split equal sum subset + lt.1049 Weight of the last stone II
Heap sort
C语言 pivot_root的Invalid argument错误解决方案
Implement vs to run only one source file at a time
Jupyter notebook displays a collection of K-line graphs
银行的理财产品一般期限是多久?
How do PMP candidates respond to the new exam outline? Look!
SSL证书格式转化的两种方法
堆排序