Commit f1fd02cc by BellCodeEditor

auto save

parent 9ee0b933
Showing with 8 additions and 12 deletions
...@@ -2,30 +2,27 @@ ...@@ -2,30 +2,27 @@
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
low=0 low=0
high=len(num_list)-1 high=len(num_list)-1
while true: while True:
mid=(low+high)//2 mid=(low+high)//2
guess=num_list[mid] guess=num_list[mid]
if guess==9: if guess==9:
print("找到9的索引了:"+str(num_list[mid])) print("找到9的索引了:"+str(num_list[mid]))
break break
elif guess>9: elif guess<9:
low=mid+1 low=mid+1
else: else:
high=mid-1 high=mid-1
if high>low: if high<low:
print("没找到9") print("没找到9")
break break
high=9
while true: while low<=high:
mid=(low+high)//2 mid=(low+high)//2
guess=num_list[mid] guess=num_list[mid]
if guess==20: if guess==20:
print("找到20的索引了:"+str(num_list[mid])) print("找到20的索引了:"+str(mid))
break break
elif guess>20: elif guess<20:
low=mid+1 low=mid+1
else: elif guess>20:
high=mid-1 high=mid-1
if high>low:
print("没找到20")
break
\ 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