程序设计爬楼梯问题_楼梯案例:解决楼梯问题的C ++程序

程序设计爬楼梯问题

A child is running up a staircase with N steps, and can hop 1 step, 2 steps or 3 steps at a time. Implement a method to count how many possible ways the child can run up to the stairs? You need to return number of possible ways W.

一个孩子正在上楼梯, 步伐N步 ,可以一次跳1步, 2步或3步。 实施一种方法来计算孩子可以上楼梯的多少种方式 ? 您需要返回可能的方式W的数量。

Input format: Line 1: Integer N (No. of steps)

输入格式:第1行:整数N(步数)

Output Format: Line 1: Integer W i.e. Number of possible ways

输出格式:第1行:整数W,即可能的方式数

Constraint: (1 <= N <= 30)

约束: (1 <= N <= 30)

Sample Input 1: 4

样本输入1: 4

Sample Output: 7

样本输出: 7

Explanation:

说明:

In this question, to find out the number of ways to we can climb the stairs, we can use a recursive method to solve it. We can call the recursive function thrice in our code with parameters of (N-1), (N-2) and (N-3) steps (the decrease in steps show the number of steps climbed). And add and return them.

在这个问题中,要找出爬楼梯的方法 ,我们可以使用递归方法来解决。 我们可以在代码中使用(N-1)(N-2)(N-3)步长的参数来调用递归函数三次(步长的减少表示爬升的步数)。 并添加并返回它们。

It is one of the typical questions for recursive algorithms.

这是递归算法的典型问题之一。

Algorithm:

算法:

  1. Step 1: Declare a recursive function staircase with one parameter (int steps).

    步骤1:声明具有一个参数的递归函数阶梯 ( int步 )。

  2. Step 2: Base Case:

    步骤2:基本案例:

    if(steps <0) // No steps to climb

    if(steps <0) //没有要爬的步骤

    return 0;

    返回0;

  3. Step 3: Base Case 2:

    步骤3:基本案例2:

    if(steps ==0) //Already reached top

    if(steps == 0) //已经到达顶部

    return 1;

    返回1;

  4. Step 4: Return staircase (steps -1) + staircase (steps – 2) + staircase (steps -3).

    步骤4:返回楼梯(步骤-1)+楼梯(步骤– 2)+楼梯(步骤-3) 。

    i.e. the total ways in which we can climb the steps.

    也就是说,我们可以爬上台阶的全部方法。

Example:

例:

    For stairs = 3.
Ways to climb are,
1 1 1
1 2
2 1
3
Hence there are four ways to climb.

C++ program:

C ++程序:

#include<bits/stdc++.h>
using namespace std;
//Recursive Function
int staircase(int n){
if(n<0){            //Base Case 1
return 0;
}
if(n==0){           //Base Case 2
return 1;
}
int count = 0;
count += staircase(n-1);    //Stepping 1 step
count += staircase(n-2);    //Stepping 2 step
count += staircase(n-3);    //Stepping 3 step
return count;
}
//Main 
int main(){
int n;
cout<<"Enter number of stairs"<<endl;
cin>>n;
cout<<"No of ways to climb stairs are ";
cout<<staircase(n)<<endl;
return 0;
}

Output

输出量

Enter number of stairs
5
No of ways to climb stairs are 13

翻译自: https://www.includehelp.com/cpp-programs/stair-case-program-to-solve-the-staircase-problem.aspx

程序设计爬楼梯问题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545845.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JDK15正式发布,新增功能预览!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;JDK 15 在 2020 年 9 月 15 号正式发布了&#xff0c;这次发布的主要功能有&#xff1a;JEP 339&#xff1a;EdDSA 数字签名…

[LeetCode] Longest Consecutive Sequence 求解

为什么80%的码农都做不了架构师&#xff1f;>>> 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, …

怎么在Word中插入歌曲

1、菜单栏 -> 插入 -> 对象 -> windows media player -> 右键 -> 属性 -> 自定义 -> 选择歌曲完整路径 -> 选择模式 -> invisible&#xff08;使视频框隐藏&#xff09;退出设计模式最后别忘了保存2、菜单栏 -> 工具 -> 宏 -> 宏 -> 宏…

双向循环链表

双向循环链表是一种较为特殊的链表&#xff0c;也是一种常见的数据结构&#xff0c;其头尾相连&#xff0c;各节点之间可互相访问&#xff0c;在单链表中&#xff0c;只能依次向后访问&#xff0c;无法访问上一个节点&#xff0c;而双链表可以依次向下访问也可向上访问。 链表…

OkHttp透明压缩,收获性能10倍,外加故障一枚

要使用OkHttp&#xff0c;一定要知道它的透明压缩&#xff0c;否则死都不知道怎么死的&#xff1b;或者活也不知道为什么活的不舒坦。反正不是好事。什么叫透明压缩呢&#xff1f;OkHttp在发送请求的时候&#xff0c;会自动加入gzip请求头Accept-Encoding:gzip。所以&#xff0…

块元素、行内块和内联元素_如何删除内联块元素之间的空间?

块元素、行内块和内联元素Introduction: 介绍&#xff1a; This question has become rather popular. How does one remove whitespaces between the inline-block elements? The interesting thing is there are numerous solutions to this but not all of them are easy …

Spring--quartz中cronExpression 的配置方法

Spring--quartz中cronExpression Java代码 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN-DEC , - * /…

C语言图形库——EasyX基本贴图

在C语言的学习过程中&#xff0c;接触最多的就是黑乎乎的DOS窗口&#xff0c;这也是在消磨学习者的兴趣&#xff0c;学到最后可能还不知道C语言到底能做什么&#xff0c;难道就是输入输出数据吗&#xff1f;当然不是&#xff0c;C的用处很广泛&#xff0c;这里不做讨论。我们能…

HTML5-特效

HTML5-特效-跟随鼠标的粒子<!DOCTYPE html> <html lang"en"><head><meta charsetutf-8><title>Liquid Particles - spielzeugz.de canvas experiment </title><meta name"description" content"HTML5/canvas …

一气之下,手撸了一个抖音去水印的工具!

百因必有果说一下我为什么要做个抖音视频去水印工具&#xff0c;其实是因为我的沙雕女友&#xff0c;她居然刚我~有天晚上她在抖音看见一个非常具有 教育意义 的视频&#xff0c;“男人疼媳妇就该承包全部家务活”&#xff0c;然后它就想把视频下载下来&#xff0c;分享到她的姐…

css 隐藏元素 显示元素_使用CSS打印时如何隐藏元素?

css 隐藏元素 显示元素Introduction: 介绍&#xff1a; We have come across many challenges while developing a website or web page and every challenge comes with new learnings. It is a trait of a good developer who develops or creates websites or web pages by…

C语言图形库——EasyX常用函数

上节讲到贴出一张图片的过程&#xff0c;本节接着介绍一些基本的图形库函数。 头文件 #include <graphics.h>1、图形窗口背景颜色设置函数&#xff1a; 例 setbkcolor(YELLOW)&#xff1b;//将图形窗口背景颜色设置为黄色。也可用 setbkcolor(RGB(180,24,137));利用三…

Adobe_Audition消除人声

傻瓜式: 方法一&#xff1a;效果 - 立体声声像 - 声道重混缩 - 选择Vocal Cut 新建左声道(左100;右-100) 新建右声道(左-100;右100;反相) 方法二&#xff1a;效果 - 立体声声像 - 析取中置通道 - Karaoke(效果预置) - 男声. (测试后&#xff0c;在傻瓜式中这种方法效果最好) 方…

Java新特性:数据类型可以扔掉了?

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在很久很久以前&#xff0c;我们写代码时要慎重的考虑变量的数据类型&#xff0c;比如下面这些&#xff1a;枚举&#xff1a…

Spyder:Python中机器学习的强大武器

So, first of all, you would need to install Anaconda distribution which can be downloaded from the link https://www.anaconda.com/download/ (for Windows users only). 因此&#xff0c;首先&#xff0c;您需要安装Anaconda发行版 &#xff0c;可以从链接https://www.…

C语言+数据结构总结

一、C语言部分 1、数据类型&#xff1a; Int 整形 4字节Short 短整形 2字节Long 长整形 4字节Float 单精度浮点型 4字节Double 双精度浮点型 8字节Char 字符型 1字节2、变量 …

对内存重叠的深入认识

内存重叠&#xff1a;拷贝的目的地址在源地址范围内。所谓内存重叠就是拷贝的目的地址和源地址有重叠。在函数strcpy和函数memcpy都没有对内存重叠做处理的&#xff0c;使用这两个函数的时候只有程序员自己保证源地址和目标地址不重叠&#xff0c;或者使用memmove函数进行内存拷…

Android特效 五种Toast具体解释

Toast是Android中用来显示显示信息的一种机制&#xff0c;和Dialog不一样的是&#xff0c;Toast是没有焦点的&#xff0c;并且Toast显示的时间有限&#xff0c;过一定的时间就会自己主动消失。 1.默认效果: 代码:Toast.makeText(getApplicationContext(), "默认Toast样式&…

为什么阿里巴巴禁止使用BigDecimal的equals方法做等值比较?

△一个对Coding有着独特追求的人△作者 l Hollis来源 l Hollis&#xff08;ID&#xff1a;hollischuang&#xff09;BigDecimal&#xff0c;相信对于很多人来说都不陌生&#xff0c;很多人都知道他的用法&#xff0c;这是一种java.math包中提供的一种可以用来进行精确运算的类型…

日期getUTCMonth()方法以及JavaScript中的示例

JavaScript Date getUTCMonth()方法 (JavaScript Date getUTCMonth() method) getUTCMonth() method is a Dates class method and it is used to get the current month’s value according to the UTC (Universal time coordinated) between the range of 0 to 11, where 0 f…