Commit a242f191 by BellCodeEditor

save project

parent 460b8c74
Showing with 15 additions and 12 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:
return mid
break break
elif d<9: elif guess<num:
b=d+1 low=mid+1
else: else:
c=d-1 high=mid-1
result=f(a,20)
\ No newline at end of file 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