Commit 4fd6b3b9 by BellCodeEditor

auto save

parent b860994f
Showing with 20 additions and 29 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1,3,5,8,11,15,17,18,20,21]
# 使用二分查找法,找出数字在列表里面的索引
num_list = [1,3,5,8,11,15,17,18,20]
low = 0
high = len(num_list)-1
while True:
mid = (low+high)//2
guess = num_list[mid]
if guess == 20:
print('找到这个数了!')
print(str(mid))
print('输入Q退出程序!')
number = input('请输入要寻找的数:')
if number == Q:
break
elif guess < 20:
low = mid+1
else:
high = mid-1
if high <= low:
print('没有这个数!')
break
low = 0
high = len(num_list)-1
while True:
mid = (low+high)//2
guess = num_list[mid]
if guess == 9:
print('找到这个数了!')
print(str(mid))
elif number != int:
break
elif guess < 9:
low = mid+1
else:
high = mid-1
if high <= low:
print('没有这个数!')
break
\ No newline at end of file
while True:
mid = (low+high)//2
guess = num_list[mid]
if guess == int(number):
print('找到这个数了!')
print('这个数在列表中的索引为'+str(mid))
break
elif guess < int(number):
low = mid+1
else:
high = mid-1
if high <= low:
print('列表中没有这个数!')
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