Commit 1aa78046 by BellCodeEditor

save project

parent f1802b65
Showing with 19 additions and 21 deletions
...@@ -4,26 +4,24 @@ alist = [] ...@@ -4,26 +4,24 @@ alist = []
for i in range(1, 101): for i in range(1, 101):
alist.append(i) alist.append(i)
num = random.choice(alist) num = random.choice(alist)
def binary_search(alist,num) def binary_search(alist,num):
num=3 num=3
guess_num=1 guess_num=1
num_list=[1,3,5,8,11,15,17,18,20,21] num_list=[1,3,5,8,11,15,17,18,20,21]
low=0 low=0
high=len(num_list)-1 high=len(alist)-1
while low<=high: while low<=high:
mid=(low+high)//2 mid=(low+high)//2
num=num_list[mid] guess=alist[mid]
if mid==guess_num: if num==guess:
print("QWQ找到了qwq") return mid
print(114514) elif guess<num:
print("yase") low=mid+1
break else:
elif mid<guess_num: high=mid-1
low=mid+1 return None
else:
high=mid-1
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num): result = binary_search(alist,num)
\ No newline at end of file print("LSGDSS",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