Im solving the recursion problem, in which the int stairs (int n) function returns the number of possibilities when climbing stairs to n, provided that either 1 step or 2 steps is performed. The following code solves the following:
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 limitation that says: if you reach the 20th floor, you MUST use an elevator that automatically takes you to the nth floor. If for some reason you miss level 20 (for example, go to level 19, and then go up 2 floors to level 21), continue as usual. Find the number of possibilities to limit above. What i have done so far:
int stairs20(int n) { if (n<=20) return stairs(n); return stairs(20) + stairs(n-21); }
The logic of the code is to calculate the number of opportunities to reach the 20th floor and the number of opportunities from the 21st floor and above. I believe that this does not return the correct answer and would be grateful for advice on where my mistake is or what I do not expect?
A elo source share