当前位置:网站首页>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;
}
如果觉得写得不错,关注点一点,下期更精彩.
边栏推荐
猜你喜欢
随机推荐
RHCE课程总结
RHCE课程总结
Where to go to IOE-EBS
微信小程序getPhoneNumber接口code=40013
C语言中的 递归问题 以及将递归改写成非递归。(解析常见的几个递归题目及代码) 求阶乘、求斐波那契、汉诺塔、
网安学习-应急响应3
iptables防火墙
display:inline-block 什么时候不会显示间隙?
*5-2 CCF 2014-12-3 集合竞价
*2-3 OJ 1164 导弹拦截之升级版
RHCE课程总结
C语言 指针的解引用详解
GridContainer of openharmony container component
*5-1 CCF 2015-03-1 图像旋转
汇编语言学习(四)
markdown学习1
ODA一体机有什么缺点
ELK部署
shell课程总结
flink并行度知识点