一、递归,在一个函数里面 调用 自己:

  pycharm的最大递归次数是997 查看与修改方法:

# # print(sys.getrecursionlimit())
# sys.setrecursionlimit(2000000000)

小例子:

# 人理解循环 神理解递归
# 算法,递归函数
# 认识递归
# 递归函数怎么读?
# 带着你写两段代码 #多大了alex,我不告诉你,alex比egon大两岁 44+2
#egon多大了,我不告诉你,egon比wusir大两岁 42+2
#wusir多大了,我不告诉你,wusir比金鑫大两岁 40+2
#金鑫40了 # age(1) n = 1 age(2)+2
# age(2) n = 2 age(3)+2
# age(3) n = 3 age(4)+2
# age(4) n = 4 40 def age(n):
if n == 4:
return 40
return age(n+1)+2 print(age(1))

二分查找算法:适用于有序的数字列表,# 典型问题:冒泡排序,快速排序,堆排序

 l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]

#二分查找算法
#
# def find(lst,aim):
# mid = len(lst)//2
# if lst:
# if aim > lst[mid]:
# new_lst = lst[mid+1:]
# find(new_lst,aim)
# elif aim < lst[mid]:
# new_lst = lst[:mid]
# find(new_lst, aim)
# else:
# print(aim,mid)
# else:
# print('您要找的值不存在')
# find(l,36)                                                                                                              
def find_2(l,aim,start=0,end=None):  #通用二分查找法[2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
if end == None:end = len(l) - 1 #start = 0,end = 24
if start <= end:
mid = (end-start) // 2 + start #mid = 12
if l[mid] > aim:
ret = find_2(l,aim,start,mid-1)
return ret
elif l[mid] < aim: #
ret = find_2(l,aim,mid+1,end) #find_2(l,58,13,24)
return ret
else:
return aim,mid
else:
print('找不到这个值')
l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
print(find_2(l,17))
#什么叫阶乘: 7  #7*6*5*4*3*2*1
def f(n):
if n == 1:
return 1
else:
return n*f(n-1)

阶乘

#斐波那契数列
# 1,1,2,3,5,8,13....
# n = 10
# f(10) = f(8)+f(9)
def fib(n):
if n == 1 or n == 2:
return 1
return fib(n-1)+fib(n-2) print(fib(100))

斐波那契数列

def f1(n,a = 1, b =1,count = 1):
count+=1
if count == n:
return b
return f1(n,b,a+b,count)
print(f1(6))

斐波那契

三级菜单

http://www.cnblogs.com/zjchao/p/7799062.html

05-26 19:43