python_递归

1.  递归示例

#coding:utf-8

#递归进行阶乘
def mm(num):
    if(num == 1):
        return 1
    else: 
        return mm(num-1) * num
    
print mm(10)

#递归进行二元查找
def search(low, uper, num, x):
    if(low == uper):
        if(num == x[low]):
            return low
        else : 
            return "None"
    mid = (low + uper) / 2
    if(num <= x[mid]):
        return search(low, mid, num, x)
    else :
        return search(mid + 1, uper, num, x)
        
def dd_search(num, x):
    low = 0
    uper = len(x) - 1
    result = search(low, uper, num, x)
    if(result == "None") :
        print "the number is not exist!"
    else :
        print "the index is %d" % result
    
seq = [4, 8, 34, 67, 95, 100, 123]
dd_search(67, seq)
dd_search(50, seq)

2. 阶乘递归的分析过程

posted @ 2016-02-15 17:13  坏脾气  阅读(276)  评论(0编辑  收藏  举报