Commit a242f191 by BellCodeEditor

save project

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