You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
我想用python寫一段用排列組合求解的程序:
class Solution:
# @param {integer} n
# @return {integer}
def climbStairs(self, n):
%%定義f(x)為求n的階乘函數(shù)。
def f(n):
c=1
for i in range(n+1):
c*=i
return c
if n==1:
return 1
elif n==2:
return 2
elif n%2==0:
sum=1
for i in range(1,n/2+1):
sum=sum+f(n-i)/(f(i)*f(n-i-i))
return sum
else:
sum=1
for i in range(1,(n+1)/2):
sum=sum+f(n-i)/(f(i)*f(n-i-i)) %(line 22)
return sum
但是網(wǎng)站返回這個(gè)錯(cuò)誤:
Runtime Error Message:
Line 22: ZeroDivisionError: integer division or modulo by zero
Last executed input:3作者: substr函數(shù) 時(shí)間: 2015-07-02 16:47 回復(fù) 1# yxycsgs