Commit 376a4ae0 by BellCodeEditor

save project

parent 8f8280f2
Showing with 63 additions and 10 deletions
a=17# 使用二分查找法,找出9和20在列表里面的索引 # a=17# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] # num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
low=0 num_list=[]
high=len(num_list)--1 for i in range(101):
def w(an): num_list.append(i)
num= random.randint(0,100)
u=0
o=len(num_list)
p=(u+o)//2
def v(li,n)
while u <= o:
y=p
if y==num:
print(p)
break
elif y<num:
o=y-1
else:
u=y+1
print(v(num_list,num))
def w(list,f):
low=0
high=len(num_list)-1
while low <= high: while low <= high:
mid=(low+high)//2 mid=(low+high)//2
g=num_list[mid] g=list[mid]
if g == a: if g == f:
print("找到了",a) # print("找到了",a)
break return f
elif g < a: elif g < f:
low =mid+1 low =mid+1
else: else:
high=mid-1 high=mid-1
print(w(num_list,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