Commit 0dfa61b1 by BellCodeEditor

save project

parent 2d52b5e4
Showing with 21 additions and 5 deletions
import random import random
alist = [] shu = []
for i in range(1, 101): for i in range(1, 101):
alist.append(i) shu.append(i)
num = random.choice(alist) num = random.choice(shu)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def a (shu,num):
xs = 0
ds = len(shu)-1
while xs <= ds:
zjds = (xs + ds) // 2
cds = shu[zjds]
if cds == num:
print('他在第',zjds + 1,'个')
return zjds
elif cds < num:
xs = zjds + 1
else:
ds = zjds - 1
def binary_search(alist,num): b = a(shu,num)
\ No newline at end of file print(num)
print(b)
\ 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