-3
nums = [734, 533, 449, 69, 869, 965, 656, 145, 913, 874, 987, 315, 967, 707]
nums.sort()
b=int(input("What number are you searching for?(Binary) "))
if b<nums[0] or b>nums[len(nums)-1]:
    print("The number you entered it out of list range")
    quit()
while True:
    if len(nums)==1 and nums[0]!=b:
        print("The number has not been found")
        break
    mid=nums[len(nums)//2]
    if mid==b:
        print("The number has been found")
        break
    elif mid>b:
        nums=nums[:len(nums)//2]
    else:
        nums=nums[len(nums)//2:]

这种二分搜索有效,但我的老师说他不想让我切片,我不知道那是什么意思。需要帮助弄清楚如何设置左右边界。

4

1 回答 1

0

让我给你一点提示

mid = (start + end) // 2
if num[mid] > b:
  end = mid
于 2013-09-22T05:47:29.330 回答