Commit 2d27ff77 by BellCodeEditor

save project

parent adaf6f8a
Showing with 16 additions and 3 deletions
import random
alist = []
alist = [1,2,3,4,5,6,7,8,9]
for i in range(1, 101):
alist.append(i)
num = random.choice(alist)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num):
\ No newline at end of file
def binary_search(alist,num):
a=0
j=len(alist)-1
while a<=j:
g=(a+j)//2
f=alist[g]
if f==9:
print('找到了')
break
elif f<9:
a=g+1
else:
j=g-1
return None
r=binary_search(alist,9)
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