Commit cf93b460 by BellCodeEditor

save project

parent fcc6d8f7
Showing with 22 additions and 18 deletions
import random
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list=[]
num=9 for i in range(1,101):
low,high=0,len(num_list)-1 num_list.append(i)
while True: suibian=random.choice(num_list)
mid=(low+high)//2
guess=num_list[mid] def zhao(num_list,num):
if guess == num: low,high=0,len(num_list)-1
print('找到了','索引',mid) while high>=low:
break mid=(low+high)//2
elif guess < num: guess=num_list[mid]
low = mid+1 if guess == num:
else: print('找到了','索引',mid)
high = mid-1 exit()
if high<low: elif guess < num:
print("找不到") low = mid+1
break else:
high = mid-1
\ No newline at end of file print("找不到")
print(suibian)
zhao(num_list,suibian)
\ 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