Staircase problem using recursion in C++
A child is running up a staircase with N steps and can hop either 1, 2 or 3 steps at a time. Implement a method to count how many possible ways the child can run-up up the stairs.
You need to return the number of possible ways W.
Example:
Input: 3
Output: 4
Explanation:
Below are the four ways
1 step + 1 step + 1 step
1 step + 2 step +
2 step + 1 step
3 step
Code:
#include
using namespace std;
int helper(int n,int count){
if(n==0)
{
return count+1;
}
else if (n<0)
{
return count;
}
count= helper(n-1,count);
count=helper(n-2,count);
count=helper(n-3,count);
}
int staircase(int n){
int temp=0;
return helper(n,temp);
}
int main() {
int n, output;
cin >> n;
output=staircase(n);
cout<< output <<endl;
}
Project Files
/
Loading...
| .. | ||
| This directory is empty. | ||