Leetcode_70_Climbing Stairs

爬楼梯问题:

设n级台阶共有F(n)种走法,因为一次可以走1~2步,所以有F(n)=F(n-1)+F(n-2),故答案的规律与斐波那契数列(1,1,2,3……)有关,由组合数学关于求通项公式的理论可知,其通项公式为

int climbStairs(int n) {
    double s=sqrt(5),x1=(1+sqrt(5))/2.0,x2=(1-sqrt(5))/2.0;
	return (int)(1/s*(pow(x1,n+1)-pow(x2,n+1)));
}

注意:本题中数列的头两项为1,2,和原数列尚有区别。

猜你喜欢

转载自blog.csdn.net/weixin_36863465/article/details/84933866