码迷,mamicode.com
首页 > 编程语言 > 详细

python--二分法算法

时间:2019-09-01 21:51:05      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:算法   tar   二分法   列表   python   discover   bsp   pytho   style   

# 二分法查找算法,必须处理有序的算法列表
"""
l = [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88]
def find(l,aim):
    mid_index = len(l) // 2
    if l[mid_index] < aim:
        new_l = l[mid_index + 1 :]
        find(new_l,aim)
    elif l[mid_index] > aim:
        new_l = l[:mid_index]
        find(new_l, aim)
    else:
        print("discover!!",mid_index,l[mid_index])
find(l,66)
"""

"""
l = [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88]
def find(l,aim,start = 0,end = None):
    end = len(l) if end is None else end
    mid_index = (end - start) // 2 + start
    if start <= end:
        if l[mid_index] < aim:
            return find(l,aim,start = mid_index + 1,end = end)
        elif l[mid_index] > aim:
            return find(l,aim,start=start,end=mid_index-1)
        else:
            return mid_index
    else:
        return ‘找不到‘
ret = find(l,66)
print(ret)
"""

# 斐波那契  # 问第n个斐波那契数是多少
# 1, 1, 2, 3, 5, 8  # fib(6) = fib(5) + fib(4)
"""
def fib(n,a=1,b=1):
    if n==1 : return a
    return fib(n-1,b,a+b)

print(fib(5))
"""

# 阶乘
"""
def fac(n):
    if n == 1 :
        return 1
    return n * fac(n-1)

print(fac(100))
"""

 

python--二分法算法

标签:算法   tar   二分法   列表   python   discover   bsp   pytho   style   

原文地址:https://www.cnblogs.com/jsit-dj-it/p/11443665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!