Commit ae7c7b74 by BellCodeEditor

save project

parent b250abfe
Showing with 16 additions and 2 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
b_n =20 #要找的
low = 0 #最低值
high = len(num_list)-1#最高
while low <=high: #条件循环
mid = (low+high)//2 #中间
g_n = num_list[mid] #中间索引的数
if g_n == b_n: #中间的数=对
print('找到了,索引是:',mid) #提示
break
elif g_n < b_n: #猜<对
low = mid+1 #最低
elif g_n > b_n: #猜>对
high = mid-1 #最高
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment