题解 | #跳台阶#
跳台阶
https://www.nowcoder.com/practice/bfb2a2b3cdbd4bd6bba0d4dca69aa3f0
#include <stdio.h>
/*
首先是n的取值范围0~40,确认0和1方法就是1,即为初值;
其次需要想清楚f(n),因为只有f(n - 1)和f(n - 2)才可以跳入f(n),所以f(n) = f(n - 1) + f(n - 2).
*/
int jump(int n)
{
if(n < 0 || n > 40)
return -1;
if (n == 0 || n == 1)
return 1;
else
{
return jump(n - 1) + jump(n - 2);
};
}
int main() {
int a;
while (scanf("%d", &a) != EOF) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
printf("%d\n", jump(a));
}
return 0;
}