上第1级: 1种上法
上第2级: 2种上法
上第3级: 3种上法 3 = 2 + 1
上第4级: 5种上法 5 = 3 + 2
上第5级: 8种上法 8 = 5 + 3
...
1,2,3,5,8, ...
从第3级楼梯开始,每级楼梯的上法等于之前两级楼梯上法的和。
由此构成斐波那契数列,登上第20级台阶种类数即为该数列第二十项的数值,经计算为10946种
#include<stdio.h>
#include<string.h>int foo(int n)
{if (1 == n) {return 1;}if (2 == n) {return 2;}if (n > 2) {return foo(n-1) + foo(n-2);}
}int main(void)
{int x = foo(20);printf("%dn", x);
}
本文发布于:2024-01-29 00:57:33,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170646106011559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |