当前位置:网站首页>"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.
边栏推荐
猜你喜欢
随机推荐
Shell编程之条件语句
'vite' is not an internal or external command, nor is it a runnable program or batch file.
2022.07.12_每日一题
《c语言小游戏》入门级三子棋游戏(机器人加强版)
安装部署KubeSphere管理kubernetes
【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
[Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
7/28-7/29 期望+思维+后缀数组+ST表
Thread 类的基本用法——一网打尽
链表实现及任务调度
2022.07.29_Daily Question
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
The Perfect Guide|How to use ODBC for Agentless Oracle Database Monitoring?
基金投顾业务
CNN--各层的介绍
2022.07.22_每日一题
Navicat new database
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
文件 - 05 下载文件:根据文件Id下载文件