Commit 8ab0fcc0 by BellCodeEditor

auto save

parent d87ba720
Showing with 4 additions and 3 deletions
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
a=9
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
a=20
low=0 low=0
high=len(num_list)-1 high=len(num_list)-1
while low<=high: while low<=high:
mid=(low+high)//2 mid=(low+high)//2
mun=num_list[mid] mun=num_list[mid]
if mun2 ==a: if a not in num_list:
print("ZHAO DAO LE",mid) print("列表中没有9")
break break
elif mun<a: elif mun<a:
low=mid+1 low=mid+1
elif mun>a: elif mun>a:
......
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