二分查找

2019-01-03  本文已影响0人  开始懂了90
# -*- coding: utf-8 -*-
li=[1,2,3,4,5,6,7,8,9,10,11,12,13]
cn = input("输入一个数:")
low = 0
high = len(li) - 1
while True:
    index = (high + low)/2
    print li[index]
    if cn == li[index]:
        print 'success'
        break
    elif cn < li[index]:
        high = index - 1
    else:
        low = index + 1


### 输出
# MacBookPro in /tmp 
$ python erfen.py
输入一个数:3
7
3
success
上一篇 下一篇

猜你喜欢

热点阅读