Im solving a recursion problem in which the function int stairs(int n) returns the number of possibilities in climbing the stairs up to n, with the conditions of either taking 1 step or 2 steps. The following code solves this:
int stairs(int n)
{
if (n<0) return 0;
if (n==0) return 1;
return stairs(n-1) + stairs(n-2);
}
Now i have a constraint that says: if you reach the 20th floor, you MUST use an elevator which takes you automatically all the way up to the n'th floor. If for some reason you skip level 20 (for instance, get to level 19 and then climb 2 floors to level 21), continue as usual. Find the number of possibilities for the constraint above. What I've done so far is this:
int stairs20(int n)
{
if (n<=20) return stairs(n);
return stairs(20) + stairs(n-21);
}
The logic behind the code is to calculate the number of possibilities in reaching the 20th floor, and the number of possibilities from the 21st floor and up. I presume this does not retrieve the correct answer and would appreciate advice on where is my mistake or what am I not calculating?