Commit 2bfc81bd by BellCodeEditor

auto save

parent 3929fc04
Showing with 13 additions and 12 deletions
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
abab = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] abab = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
low=0 low=0
high=len(abab)-1 def b
num=int(input("输入:")) high=len(abab)-1
while high>low: num=int(input("输入:"))
mid=(low+high)//2 while high>low:
if num==abab[mid]: mid=(low+high)//2
print("找到了") if num==abab[mid]:
break print("找到了")
elif abab[mid]>num: break
high=abab[mid]-1 elif abab[mid]>num:
else: high=abab[mid]-1
low=abab[mid]+1 else:
\ No newline at end of file low=abab[mid]+1
\ 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