Commit 7d16dfd8 by BellCodeEditor

save project

parent 356882fb
Showing with 14 additions and 12 deletions
...@@ -2,19 +2,21 @@ ...@@ -2,19 +2,21 @@
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
high=len(num_list)-1 high=len(num_list)-1
low=0 low=0
mid=(high+low)//2
n=20 n=2
while True: while low <= high:
if num_list[mid]<n: mid = (low+high)//2
low=mid+1 guess = num_list[mid]
if guess==n:
print('找到'+str(n)+'了,索引为'+str(mid))
break
elif num_list[mid]>n: elif guess>n:
high=mid-1 high=mid-1
elif num_list[mid]==n: elif guess<n:
print('找到'+str(n)+'了,索引为'+'str(mid)') low = mid +1
break if high<low:
elif high<=low: print('找不到')
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