Commit 327d0e70 by BellCodeEditor

auto save

parent 79fcccb9
Showing with 18 additions and 15 deletions
# 使用二分查找法,找出9和20在列表里面的索引
from random import *
b = randint(1,1000000)
l = 1
h = 1000000
while l <= h:
m = (l + h) // 2
if m == b:
print(str(b) + '找到了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!,' + str(m))
break
elif m > b:
h = m-1
elif m < b:
l = m + 1
else:
print('找不到!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
a = [94,91,90,88,87,83,81,80,79,77,76]
#for i in range(1,10001):
#a.append(i)
n = 88
def b (a,n):
l = 0
h = len(a)-1
while l <= h:
m = (l + h) // 2
c = a[m]
if c == n:
return m
elif c > n:
l = m + 1
elif c < n:
h = m-1
return None
print(b(a,88))
......
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