Commit 59fb02a7 by BellCodeEditor

save project

parent adaf6f8a
Showing with 21 additions and 5 deletions
import random
alist = []
alist_a = []
for i in range(1, 101):
alist.append(i)
num = random.choice(alist)
alist_a.append(i)
num_zhao = random.choice(alist_a)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num):
\ No newline at end of file
def binary_search(alist,num):
low=0
haight=len(alist)-1
while low<=haight:
mid=(low+haight)//2
gusse_cai=alist[mid]
if num==gusse_cai:
return mid
elif num>gusse_cai:
low=mid+1
elif num<gusse_cai:
haight=mid-1
if low>haight:
return None
ep=binary_search(alist_a,num_zhao)
print(ep)
print(alist_a[ep])
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