擅长:python、mysql、java
<p>下面的代码片段将按您的要求执行,尽管方式相对笨拙(但更容易理解):</p>
<pre><code># Ask the user for an input
y = int(input("Enter a number: "))
# List of numbers to be searched
numblist = [5, 8, 9, 10, 18, 20, 25, 28, 30, 35]
# Set the lower bound of the search
lowerval = numblist[0]
# Set the upper bound of the search
higherval = numblist[-1]
# Search Algorithm
while True:
mid = (lowerval + higherval) // 2
if mid == y:
# Here, I am assuming that you will only test against a list with only unique values.
# It first indexes the value that was found, then subtracts one from it to obtain the value prior to the found value.
print(numblist[numblist.index(y) - 1])
# Then it exits the while loop.
break
if mid < y:
lowerval = mid
if mid > y:
higherval = mid
</code></pre>
<p>希望这有帮助!你知道吗</p>