- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Climbing Stairs in C++
There are n stairs. One person will go to 1st to nth stairs. Maximum how many stairs he/she can cross in one step is also given. With this information, we have to find possible ways to go to the nth stairs. Let us consider one can cross a maximum two stairs in each step. So we can find recursive relations to solve this problem. One can move to nth stair, either from (n-1)th stair or from (n-2)th stair. So ways(n) = ways(n-1) + ways(n-2).
Suppose the number of stairs, say 10, the maximum number of stairs that can be jumped in one step, say 2, then the output will be 89 possible ways.
To solve this, follow these steps −
- define array count of size same as stair number
- count[0] := 1
- for i := 2 to stair -1, do
- count[i] := 0
- for j = 1 to i and j <= max; do
- count[i] := count[i] + count[i - j]
- return count[stair - 1]
Let us see the implementation to get better understanding
Example (C++)
#include<iostream> using namespace std; int stairClimbWays(int stair, int max){ int count[stair]; //fill the result stair using bottom up manner count[0] = 1; //when there are 0 or 1 stair, 1 way to climb count[1] = 1; for (int i=2; i<stair; i++){ //for stair 2 to higher count[i] = 0; for(int j=1; j<=max && j<=i; j++) count[i] += count[i-j]; } return count[stair-1]; } int countWays(int stair, int max){ //person can climb 1,2,...max stairs at a time return stairClimbWays(stair+1, max); } int main (){ int stair, max; cout << "Enter number of stairs: "; cin >> stair; cout << "Enter max stair a person can climb: "; cin >> max; cout << "Number of ways to reach: " << countWays(stair, max); }
Input
Stairs = 10 Max stairs a person can climb: 2
Output
Enter number of stairs: 10 Enter max stair a person can climb: 2 Number of ways to reach: 89
- Related Articles
- Min Cost Climbing Stairs in Python
- How to implement backtracking for a climbing stairs practice in JavaScript?
- Reena climbs up five stairs every second and than climb down two stairs over the next second .how many seconds will she take to climb 60 stairs?
- A man whose mass is $50 kg$ climbs up $30$ steps of a stair in $30 s$. If each step is $20 cm$ high, calculate the power used in climbing the stairs. [Take $g=10 ms^{-2}$]
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Program to find how many ways we can climb stairs in Python
- Name the material used for making ropes for rock climbing.
- Name the fibre used for making parachutes and rock climbing ropes.
- Program to find to get minimum cost to climb at the top of stairs in Python?
- Program to find number of ways we can reach to the next floor using stairs in Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Why it is necessary for mountaineers climbing high mountains to carry oxygen cylinders are with them?
- State whether the following objects possess kinetic energy, potential energy, or both:(a) A man climbing a hill(b) A flying aeroplane(c) A bird running on the ground
- A man weighing 500 N carried a load of 100 N up a flight of stairs 4 m high in 5 seconds. What is the power?
- Explain why:(a) a pencil will write on paper but not on glass.(b) climbing a greasy pole is very difficult.

Advertisements