#递归函数
# 了解什么是递归 : 在函数中调用自身函数
# 最大递归深度默认是997/998 —— 是python从内存角度出发做得限制
# 能看懂递归
# 能知道递归的应用场景
# 初识递归 ——
# 算法 —— 二分查找算法
# 三级菜单 —— 递归实现
# while True:
# print('从前有座山')
# def story():
# print('从前有座山')
# story()
# print(111)
#
# story()
#RecursionError: maximum recursion depth exceeded while calling a Python object
# 递归的错误,超过了递归的最大深度
# import sys
# sys.setrecursionlimit(1000000)
# n = 0
# def story():
# global n
# n += 1
# print(n)
# story()
# story()
# 如果递归次数太多,就不适合使用递归来解决问题
# 递归的缺点 : 占内存
# 递归的优点: 会让代码变简单
# alex 多大 n = 1 age(1) = age(2)+2 = age(n+1) + 2
# alex比egon大两岁
# egon多大? n = 2 age(2) = age(3) + 2 = age(n+1) +2
# egon比wusir大两岁
# wusir多大 n = 3 age(3) = age(4) + 2 = age(n+1) +2
# wusir比金老板大两岁
# 金老板多大?
# 金老板40了 n = 4 age(4) = 40
# n = 4 age(4) = 40
# n <4 age(n) = age(n+1) +2
def age(n):
if n == 4:
return 40
elif n >0 and n < 4:
age(n+1) + 2
#
print(age(1))
# # 教你看递归
# def age(1):
# if 1 == 4:
# return 40
# elif 1 > 0 and 1 < 4:
# return 46
#
# def age(2):
# if 2 == 4:
# return 40
# elif 2 >0 and 2 < 4:
# age(3) + 2 None +2
#
# def age(3):
# if 3 == 4:
# return 40
# elif 3 >0 and 3 < 4:
# 42
#
# def age(4):
# if 4 == 4:
# return 40
# elif n >0 and n < 4:
# age(n+1) + 2
# 什么叫算法
# 计算的方法 : 人脑复杂 计算机简单
# 99 * 13 = 1287 = 13*100 - 13
# 查找 : 找数据
# 排序 :
# 最短路径
# 我们学习的算法 都是过去时
# 了解基础的算法 才能创造出更好的算法
# 不是所有的事情都能套用现成的方法解决的
# 有些时候会用到学过的算法知识来解决新的问题
# 二分查找算法 必须处理有序的列表
# 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]
# 5000000 4999998
# 代码实现
# def find(l,aim):
# mid_index = len(l) // 2
# if l[mid_index] < aim:
# new_l = l[mid_index+1 :]
# find(new_l,aim)
# elif l[mid_index] > aim:
# new_l = l[:mid_index]
# find(new_l, aim)
# else:
# print('找到了',mid_index,l[mid_index])
#
# find(l,66)
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(l,aim,start = 0,end = None):
# end = len(l) if end is None else end # end = len(l) 24
# mid_index = (end - start)//2 + start #计算中间值 12 + 0 = 12
# if l[mid_index] < aim: #l[12] < 44 #41 < 44
# find(l,aim,start =mid_index+1,end=end) # find(l,44,start=13,end=24)
# elif l[mid_index] > aim:
# find(l, aim, start=start, end=mid_index-1)
# else:
# print('找到了',mid_index,aim)
#
# def find(l,aim,start = 0,end = None): # l,44,start=13,end=24
# end = len(l) if end is None else end # end = 24
# mid_index = (end - start)//2 + start #计算中间值 24-13/2 = 5 + 13 = 18
# if l[mid_index] < aim: #l[18] < 44 #67 < 44
# find(l,aim,start =mid_index+1,end=end)
# elif l[mid_index] > aim: # 67 > 44
# find(l, aim, start=start, end=mid_index-1) # find(l,44,start=13,end=17)
# else:
# print('找到了',mid_index,aim)
#
# def find(l,aim,start = 0,end = None): # l,44,start=13,end=17
# end = len(l) if end is None else end # end = 17
# mid_index = (end - start)//2 + start #计算中间值 17-13/2 = 2 + 13 = 15
# if l[mid_index] < aim: #l[15] < 44 #55 < 44
# find(l,aim,start =mid_index+1,end=end)
# elif l[mid_index] > aim: # 55 > 44
# find(l, aim, start=start, end=mid_index-1) # find(l,44,start=13,end=14)
# else:
# print('找到了',mid_index,aim)
#
# def find(l,aim,start = 0,end = None): # l,44,start=13,end=14
# end = len(l) if end is None else end # end = 14
# mid_index = (end - start)//2 + start #计算中间值 14-13/2 = 0+ 13 = 13
# if l[mid_index] < aim: #l[13] < 44 #42 < 44
# find(l,aim,start =mid_index+1,end=end) # find(l,44,start=14,end=14)
# elif l[mid_index] > aim: # 42 > 44
# find(l, aim, start=start, end=mid_index-1)
# else:
# print('找到了',mid_index,aim)
def find(l,aim,start = 0,end = None):
end = len(l) if end is None else end
mid_index = (end - start)//2 + start
if start <= end:
if l[mid_index] < aim:
return find(l,aim,start =mid_index+1,end=end)
elif l[mid_index] > aim:
return find(l, aim, start=start, end=mid_index-1)
else:
return mid_index
else:
return '找不到这个值'
ret= find(l,44)
print(ret)
# 参数 end
# 返回值
# 找不到的话怎么办
# l.index()
# 67 发生两次调用
# 66 发生好几次
# 44 找不到
# age,二分查找,三级菜单的代码看一遍
# 斐波那契 # 问第n个斐波那契数是多少
# 阶乘
#3! 3*2*1
# 附加题 :考试附加题
# 递归实现