当前位置:网站首页>"C language" frog jumping steps recursion problem
"C language" frog jumping steps recursion problem
2022-07-31 07:58:00 【Guo Guo study tour】
Title:
A frog can jump 1 step at a time or jump 2 steps at a time. For example, there is only one way to jump to the first step: just jump 1 step directly.There are two ways to jump up two steps: one step at a time, two jumps; or two steps at a time. How many ways are there to jump to the nth step?
Thinking Analysis:
First of all, we can consider that the frog will only have one jumping method when there is only one step, and two jumping methods when there are two steps
Then let's try to consider if there are three steps, the little frog can have a variety of choices, he can choose one by one

You can also choose to jump one step for the first time and jump two steps directly for the second time

You can also choose to jump two steps for the first time, and then jump another step

In summary, we can get
If there is a step, the little frog has a way of jumping;
If there are two steps, the frog can jump in two ways;
If there are three steps, the frog can jump in three ways;
If there are four steps, the frog can jump in five ways;
……………………
Then we can consider that the jumping method of the little frog is nothing more than the number of jumps of n-1 steps + the number of jumps of n-2 steps
So our code can look like this
int frog(int n) {if (n == 1)return 1;else if (n == 2)return 2;else return frog(n - 1) + frog(n - 2);}Okay, I will share this idea here~~ Thank you for your support.
边栏推荐
- 客户端navicat安装教程
- Pygame Surface对象
- 《c语言》青蛙跳台阶递归问题
- 文件 - 02 上传文件:上传临时文件到服务器
- 【面试题】从输入URL到游览器渲染完成,经历了什么
- 2022.07.14_每日一题
- Thread 类的基本用法——一网打尽
- 《opencv学习笔记》-- 仿射变换
- Calculation example of matlab program iEEE9 node system for power flow calculation of AC-DC hybrid system based on alternate iteration method
- 2022.07.20_Daily Question
猜你喜欢

shell/bash脚本命令教程

'vite' is not an internal or external command, nor is it a runnable program or batch file.

XSS靶场prompt.ml过关详解

【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用

2022.07.20_Daily Question
![[PSQL] SQL基础教程读书笔记(Chapter1-4)](/img/76/d416f79b7f2c93c1c79a285c30d3e6.png)
[PSQL] SQL基础教程读书笔记(Chapter1-4)

2022.07.13_每日一题

正则表达式绕过

MySQL installation to the last step in the write the configuration file failed?And after the installation steps

DAY18: XSS vulnerability
随机推荐
【Objective-C语言中的@property】
把 VS Code 当游戏机
2022.07.14_Daily Question
MySQL安装到最后一步 write configuration file 失败 怎么办?及后安装步骤
文件 - 03 下载文件:根据文件id获取下载链接
navicat 新建数据库
2022.07.20_每日一题
CNN--Introduction to each layer
SCI写作指南
2022.07.15_Daily Question
Vscode: Project-tree plugin
2022.07.12_Daily Question
最大似然估计和最小二乘法 含代码
【Go】Go 语言切片(Slice)
页面懒加载
2022.07.14_每日一题
文件 - 02 上传文件:上传临时文件到服务器
2022.07.20_每日一题
【面试题】从输入URL到游览器渲染完成,经历了什么
分布式缓存系统必须要解决的四大问题