递归浅析

递归:

1.函数调用本身

2.要有明确的结束标志

例:求1到指定数字的和(100)

sum(100) = sum(99) + 100

sum(99) = sum(98) + 99

......

sum(2) = sum(1) + 2

sum(1) = 1

def sum(num):
    # 设置结束标志
    if num == 1:
        return 1
    return sum(num-1) + num

递归嵌套最多1000层(python中)

 

  

posted @ 2019-03-15 21:35  胖白白  阅读(132)  评论(0编辑  收藏  举报