当前位置:网站首页>"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.
边栏推荐
- 解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
- Thread 类的基本用法——一网打尽
- 2022.07.26_Daily Question
- 【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
- 链表实现及任务调度
- ros小乌龟画图
- 'vite' is not an internal or external command, nor is it a runnable program or batch file.
- 2022.07.13_每日一题
- 2022.7.29 Array
- 进程调度的基本过程
猜你喜欢
Environment_Variable_and_SetUID
‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
NK-RTU980烧写裸机程序
2022.07.29_Daily Question
电压源的电路分析知识分享
机器学习---线性回归、Logistic回归问题相关笔记及实现
Machine Learning - Notes and Implementation of Linear Regression, Logistic Regression Problems
2022.07.12_每日一题
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
【微服务】Nacos集群搭建以及加载文件配置
随机推荐
XSS靶场prompt.ml过关详解
《opencv学习笔记》-- 仿射变换
Super detailed mysql database installation guide
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
Pygame Surface对象
Ceph单节点部署
DAY18:Xss 靶场通关手册
2022.07.18_每日一题
分布式缓存系统必须要解决的四大问题
Client navicat installation tutorial
'vite' is not an internal or external command, nor is it a runnable program or batch file.
Tasks and task switching
2022.07.22_每日一题
项目 - 如何根据最近30天、最近14天、最近7天、最近24小时、自定义时间范围查询MySQL中的数据?
2022.07.14_每日一题
2022.07.24_Daily Question
【解决】npm ERR A complete log of this run can be found in npm ERR
Collation and sharing of related classic papers and datasets in the field of deep learning communication
Financial leasing business
2022.07.20_Daily Question