参考:
https://www.jianshu.com/p/74cdb5d8d264
改成Python:
1 def go_upstairs(n): 2 if(n == 0): 3 return 0 4 elif(n == 1): 5 return 1 6 elif(n == 2): 7 return 2 8 elif(n == 3): 9 return 4 10 elif(n > 3): 11 return go_upstairs(n - 1) + go_upstairs(n - 2) + go_upstairs(n - 3) 12 13 number_of_steps = 6#楼梯阶数 14 print("%d级阶梯,有%d中上楼方法"%(number_of_steps, go_upstairs(number_of_steps)))
执行结果:
6级阶梯,有24中上楼方法
Process finished with exit code 0