递归原理及实现

递归:

1.在过程或函数中调用自身

2.必须有一个递归结束条件----递归出口

3.递归算法解题简洁但运行效率低,占内存容易栈溢出

def calc(n):

  print(n)

  if n/2>1:

    res= calc(n/2)

    print('res',res)

  print("N",n)

  return n

calc(10)

通过递归实现斐波那契数列

def func (arg1,arg2,stop):

  if arg1==0:

    print arg1,arg2

  arg3=arg1+arg2

  print arg3

  if arg3>stop:

    func(arg2.arg3,stop)

func(0,1,30)

  

原文地址:https://www.cnblogs.com/my334420/p/6393673.html