Commit a242f191 by BellCodeEditor

save project

parent 460b8c74
Showing with 17 additions and 14 deletions
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
a = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] a = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
b=0 def f(a,num):
c=len(a)-1 low=0
high=len(a)-1
while b<=c: while low<=high:
d=(b+c)//2 mid=(low+high)//2
if d==9: guess=a[mid]
print("索引是:",d) if guess==num:
break return mid
elif d<9: break
b=d+1 elif guess<num:
else: low=mid+1
c=d-1 else:
high=mid-1
\ No newline at end of file result=f(a,20)
print("老师给的数:"20)
print("索引是:",result)
\ No newline at end of file
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