lst = []
for i in range(1,5000,3):
lst.append(i)
num = int(input('请输入一个数:'))
left = 0
right = len(lst) - 1
count = 0
while left <= right:
count += 1
mid = (right + left) // 2
if num < lst[mid]:
right = mid - 1
elif num > lst[mid]:
left = mid + 1
else:
print('find it')
break
else:
print('can not find it')
print(count)
lst = []
for i in range(1,5000,3):
lst.append(i)
num = int(input('请输入一个数:'))
left = 0
right = len(lst) - 1
count = 0
while left <= right:
count += 1
mid = (right + left) // 2
if num < lst[mid]:
right = mid - 1
elif num > lst[mid]:
left = mid + 1
else:
print('find it')
break
else:
print('can not find it')
print(count)
二分法代码