当前位置:网站首页>Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
2022-07-30 19:36:00 【Fruit Chenchen】
小青蛙跳台阶
一、 问题描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法???
二、分析问题
当n=1时,有一种跳法
当n=2时,有两种跳法
1.跳一级,跳一级
2.跳两级
当n=3时,There are three jumps
1.跳一级,跳一级,跳一级
2.跳一级,跳两级
3.跳两级,跳一级
先假设f(n)为 n 级台阶的总跳法数;
Then if you choose to skip one level for the first time,剩下的 n-1 The number of jumps for each step is f(n−1).
If you jump two levels for the first time,剩下的 n-2 级台阶的跳法就是f(n−2);
所以,当有nThere are stepsf(n)=f(n-1)+f(n-2)种跳法.
三、代码实现(递归)
#include<stdio.h>
int steps(int x)
{
if (x == 1)
{
return 1;
}
if (x == 2)
{
return 2;
}
return steps(x - 1) + steps(x - 2);
}
int main()
{
int n = 0;
scanf("%d", &n);
printf("%d", steps(n));
return 0;
}
四、代码实现(非递归)
#include<stdio.h>
int steps(int x)
{
if (x < 3)
{
return x;
}
int final1 = 1;//Countdown to the first jump
int final2 = 2;//The penultimate jump
int sum = 0;//Count how many in total
for (int i = 3; i <= x; i++)
{
sum = final1 + final2;
final1 = final2;
final2 = sum;
}
return sum;
}
int main()
{
int n = 0;
scanf("%d", &n);
printf("%d", steps(n));
return 0;
}
边栏推荐
- VBA batch import Excel data into Access database
- Perfectly Clear QuickDesk & QuickServer图像校正优化工具
- MySQL八股文背诵版
- Trial writing C language sanbang
- Day31 LeetCode
- 实体中增加操作方法
- MySQl数据库————DQL数据查询语言
- Node encapsulates a console progress bar plugin
- 【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
- Zabbix 5.0 监控教程(一)
猜你喜欢
redis
Encapsulates a console file selector based on inquirer
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
MindSpore:【语音识别】DFCNN网络训练loss不收敛
Linux下最新版MySQL 8.0的下载与安装(详细步骤)
谷歌AlphaFold近日宣称预测出地球上几乎所有蛋白质结构
Download Win11 how to change the default path?Download Win11 change the default path method
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
随机推荐
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
VS Code connects to SQL Server
The technology is very powerful, do you still need to "manage up"?
crontab中写go run不执行的问题
Another company interview
MySQl数据库————DQL数据查询语言
Vulkan开启特征(feature)的正确姿势
MindSpore:【JupyterLab】按照新手教程训练时报错
The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
电脑死机的时候,发生了什么?
JsonUtil基于字符串操作josn
nlohmann json 使用指南【visual studio 2022】
What is the value of biomedical papers? How to translate the papers into Chinese and English?
跨域问题的解决方法
requet.getHeader("token") is null
【网站放大镜效果】两种方式实现
监听开机广播
MindSpore:npu 多卡训练自定义数据集如何给不同npu传递不同数据
Go system collection