Commit bf467070 by BellCodeEditor

save project

parent adaf6f8a
Showing with 24 additions and 7 deletions
import random import random
alist = [] def binary_search(alist,bingo):
for i in range(1, 101): low=0
alist.append(i) high=len(alist)-1
num = random.choice(alist) while low<=high:
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 mid=(low+high)//2
guess=alist[mid]
if bingo==guess:
return mid
elif guess<bingo:
low=mid+1
else:
high=mid-1
return None
def binary_search(alist,num): guess_list=[]
\ No newline at end of file for i in range(20):
if random.randint(0,2)==2:
guess_list.append(i)
bingo=guess_list[random.randint(0,len(guess_list)-1)]
result=binary_search(guess_list,bingo)
print('查找结束,猜的值是:',bingo)
print('它的索引是:',result)
print(guess_list)
\ 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