Python递归二分法

# lst = [4, 56, 178, 253, 625, 1475, 2580, 3574, 15963] # 时间复杂度. n
# # 让用户输入一个数n. 判断这个n是否出现在lst中
# n = int(input("请输入一个数字n:")) # 56
# for el in lst:
# if n == el:
# print('出现了')
# break
# else:
# print("没出现")


# 使用二分法查找来实现上述功能,
# 必须是有序序列
# print(2**28) # 268435456
# print(2**26) # 134217728



# lst = [4, 56, 178, 253, 625, 1475, 2580, 3574, 15963]
#
# n = int(input("请输入一个数字n:")) # 56
#
# left = 0 # 左边界
# right = len(lst) - 1 # 末尾的索引 右边界
# while left <= right: # 当左边界大于右边界结束循环
#
# mid = (left + right) // 2 # 求中间的索引坐标
# if n < lst[mid]: # 判断你的数字和中间数的大小比较 .
# right = mid - 1 # 右边界往左移动
# elif n > lst[mid]:
# left = mid + 1 # 左边界往右移动
# else:
# print("找到了") # 找到了目标数字
# break
# else: # 当左比右大, 循环结束. 没有找到目标数
# print("没找到")
#

# 0 1 2 3 4 5 6 7 8
# lst = [4, 56, 178, 253, 625, 1475, 2580, 3574, 15963]

# def binary_search(lst, n, left, right):
# if left > right:
# return False
# mid = (left + right) // 2
# if n > lst[mid]:
# left = mid + 1
# # 当递归有返回值的时候. 需要写return. 否则有可能接收不到返回值
# return binary_search(lst, n, left, right)
# elif n < lst[mid]:
# right = mid - 1
# return binary_search(lst, n, left, right)
# else:
# print("找到了")
# return True
#
#
# n = int(input("请输入一个数字n:")) # 178
# ret = binary_search(lst, n, 0, len(lst)-1)
# print(ret)




# 切换列表
# def binary_search(lst, n):
# if len(lst) == 0:
# return False
# left = 0
# right = len(lst) - 1
# mid = (left + right) // 2
# if n > lst[mid]:
# left = mid + 1
# # 当递归有返回值的时候. 需要写return. 否则有可能接收不到返回值
# return binary_search(lst[mid+1:], n)
# elif n < lst[mid]:
# right = mid - 1
# return binary_search(lst[:mid], n)
# else:
# print("找到了")
# return True
#
#
# n = int(input("请输入一个数字n:")) # 178
# ret = binary_search(lst, n)
# print(ret)


# 递归深度
# def func():
# print("哈哈")
# func()
# func()
# python中最大的递归深度是1000 但是你永远到不了1000 998左右不同系统不一样
import sys # system python, os 操作系统
print(sys.getrecursionlimit()) #查询最大递归深度
sys.setrecursionlimit(3000) # 设置最大递归深度 最大是3000 你到不了3000



猜你喜欢

转载自www.cnblogs.com/searchforyou/p/9917654.html