Commit 1ddff148 by BellCodeEditor

save project

parent 69686bfa
Showing with 21 additions and 3 deletions
......@@ -5,5 +5,23 @@ 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 f1(alist,num):
# 使用二分查找法,找出9和20在列表里面的索引
nh=0
min=0
max=len(alist)-1
while min<=max:
mid=(min+max)//2
ges=alist[mid]
if ges==num:
print('ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh')
print('第',mid,'位')
nh=1
break
if ges>num:
max=mid-1
if ges<num:
min=mid+1
if nh==0:
print('noooooooooooooooooooooooooooooooooooooooooooooooo')
f1(alist,num)
\ 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