Commit 7a6548d7 by BellCodeEditor

save project

parent b250abfe
Showing with 22 additions and 2 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file
import random
f=[]
for i in range(1,10001):
f.append(i)
c=random.choice(f)
def g(f,c):
a=0
b=len(f)-1
while a<=b:
d=(a+b)//2
e=f[d]
if e == c:
return d
if e<c:
a=d+1
else:
b=d-1
return None
a1=g(f,c)
print("老师给的数是",c)
print("它的索引是",a1)
\ 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