Commit 3e9c4a3c by BellCodeEditor

save project

parent b250abfe
Showing with 16 additions and 2 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file
a = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
n=int(input('请输入你想查找的数字:'))
start=0
end=len(a)-1
while True:
mid=(start+end)//2
if n > a[mid]:
start=mid+1
elif n==a[mid]:
print(n,'在',mid,'号位置')
break
elif n<a[mid]:
end=mid-1
if start > end:
print(n,'不存在')
break
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