当前位置:网站首页>frog jumping steps
frog jumping steps
2022-08-09 14:47:00 【GD_small_bit】
今天,我将为大家带来青蛙跳台阶的程序,Implemented recursively and non-recursively, respectively,话不多说,直接开始主题.
文章目录
Frog jumping steps game explanation
问题描述:in front of the frogN个台阶,And the frog can only jump every time1~2级的台阶,那么请问,How many ways a frog can jump onN级的台阶.
Let's say our steps start with1级,那么毫无疑问,Frogs can only jump up the steps in one way.
Suppose we have two steps,Then the frog can get to the top of our steps in two ways.第一种,The frog jumps step by step to the top;第二种,The frog jumped two levels straight to the top.
Suppose we have three steps,Then the frog has three ways to jump to the top of the steps.第一种,Select a level to jump to the top;第二种,Choose to jump two steps first,Jump one more step to the top;第三种,Choose to jump one stair first,Jump two more steps to the top.
Suppose we have four steps,Then the frog has five ways to get to the top.第一种,Jump level by level to the top;第二种,Jump two levels first,One more jump two levels;第三种,Skip level two first,Jump two more levels;第四种,先跳一级,Skip two more levels,再跳一级;第五种,Jump two levels directly.
当N为1时,方法为1;
当N为2时,方法为2;
当N为3时,方法为3;
当N为4时,方法为5;
…
…
…
观察可以得知,当有N级台阶时,The method is yesN-1steps andN-2The sum of the step methods.
Recursive implementation of frog jumping steps
#include<stdio.h>
int frog (int n)
{
if(n==1)
{
return 1;
}
else if(n==2)
{
return 2;
}
else
{
return frog(n-1)+frog(n-2);
}
}
int main ()
{
int N = 0;
int ret = 0;
scanf("%d",&N);
ret = frog(N);
printf("%d",ret);
return 0;
}
非Recursive implementation of frog jumping steps
```c
#include<stdio.h>
int frog (int n)
{
int a = 1;
int b = 2;
int c = 0;
if(n==1)
{
return a;
}
else if(n==2)
{
return b;
}
else
{
while(n>=3)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
}
int main ()
{
int N = 0;
int ret = 0;
scanf("%d",&N);
ret = frog(N);
printf("%d",ret);
return 0;
}
如果觉得写得不错,关注点一点,下期更精彩.
边栏推荐
猜你喜欢
随机推荐
C语言 三子棋(含完整 代码详解)
小程序程序开发怎么做?应以突出功能为主
Kubernetes资源编排系列之三: Kustomize篇
RHCE课程总结
RHCE课程总结
汇编语言学习(八)
typeorm 批量插入数据优化和插入冲突操作
*2-2 OJ 1163 导弹拦截之测试版
网站小程序开发有哪些步骤?
Jetpack Compose - Image using Coli load network Image (including GIF, SVG)
青蛙跳台阶
iptables之SNAT与DNAT
C语言 求一个整数存储在内存中的二进制中1的个数(多种方法详解)
Mysql seven connection query methods
Dry+Bean+Dataset R language data analysis, report in English
微信小程序getPhoneNumber接口code=40013
RHCE课程总结
系统可扩展性思考
如何用vs新建Asp.net项目(Web页面)
*5-2 CCF 2014-12-3 集合竞价









