-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclimdstairs.cpp
More file actions
40 lines (34 loc) · 1.09 KB
/
climdstairs.cpp
File metadata and controls
40 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Write a C++ program to calculate the number of ways to climb stairs (with varying step sizes).
#include <iostream>
using namespace std;
//Function to cout the number of ways
int countWays(int* steps, int numSteps, int n) {
if (n < 0) {
return 0;
} else if (n == 0) {
return 1;
}
int ways = 0;
for (int i = 0; i < numSteps; ++i) {
ways += countWays(steps, numSteps, n - steps[i]);
}
return ways;
}
int main() {
//input a number of staries,number of step sizes and number of step from the user
int num;
cout << "Enter the number of stairs: ";
cin >> num;
int numSteps;
cout << "Enter the number of different step sizes: ";
cin >> numSteps;
int steps[numSteps];
cout << "Enter the step sizes " << endl;
for (int i = 0; i < numSteps; ++i) {
cout << "Enter " << i+1 << "th step size: ";
cin >> steps[i];
}
//Displaying the number to the climd staries by calling a countways function
cout << "Number of ways to climb the stairs: " << countWays(steps, numSteps, num) << endl;
return 0;
}