python二分法
l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
left = 0
right = len(l1) - 1
a = int(input("Please input a number:"))
while left <= right:
mid = (left + right) // 2
if a > l1[mid]:
left = mid + 1
elif a < l1[mid]:
right = mid - 1
else:
print(str(a)+"is in the index of "+str(mid))
break
else:
print('404 NOT FOUND')
本文来自博客园,作者:积分别忘C,转载请注明原文链接:https://www.cnblogs.com/hackzz/p/18389546