Commit 5ff4ea97 by BellCodeEditor

save project

parent 002f477b
Showing with 9 additions and 8 deletions
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
import random import random
n=random.randint(1,100)
alist=[] alist=[]
n=1 num=1
low=1 low=1
high=len(alist-1) high=len(alist-1)
for i in range(1,101): for i in range(1,101):
alist.append(n) alist.append(num)
n+=1 num+=1
while True: while True:
mid=(low+high)//2 n=(low+high)//2
if n==num_list[mid]: if n==alist[n]:
print(mid) print(n)
break break
elif n < num_list[mid]: elif n < alist[n]:
low=mid+1 low=n+1
else: else:
high=n-1 high=n-1
break break
......
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