def func(n):
if n == 1:
return 1
else:
return n * func(n-1)
def fibo(n):
if n == 1 or n == 2:
return 1
else:
return fibo(n-1) + fibo(n-2)
def b_sort(l, aim, start=0, end=None):
if end == None: end = len(l)-1
if start <= end:
mid = (end-start) // 2 + start #保证每次都是相应的数列位置
if aim < l[mid]:
return b_sort(l, aim, start, mid-1)
elif l[mid] < aim:
return b_sort(l, aim, mid+1, end)
else:
return mid,aim
else:
print('您输入的值不存在!')
不用递归的二分查找
def binary_sort(l, aim, start=0, end=None):
if end = None: end = len(l)-1
while start<=end:
mid = (end+start)//2
if aim < l[mid]:
end = mid - 1
elif l[mid] < aim:
start = mid + 1
else:
return mid
def move(n, a, b, c):
if n == 1:
print('{}-->{}'.format(a,c))
return
move(n-1, a, c, b) # 将汉诺塔看成最底下和其他两层,先将上面的移到B位置
move(1, a, b, c) # 将最底下的移到C位置
move(n-1, b, a, c) # 将上面的从B位置移到C位置 层层递归都是这样