"""
一开始有5瓶啤酒
两个空酒瓶换一瓶啤酒
四个瓶盖换一瓶啤酒
问你最后能够喝到多少瓶啤酒
"""
def get(n):
bottle, head = n, n
drink = 0
while 1:
drink += bottle
use = bottle // 2 + head // 4
if use == 0:
break
bottle %= 2
head %= 4
bottle += use
head += use
return drink
def recursive(bottle, head):
"""
use=b//2+head//4
f(b,h)=f(use+bottle%2,use+head%4)+bottle
利用这个递推式可能能找到O(1)的算法
:param bottle:
:param head:
:return:
"""
use = bottle // 2 + head // 4
if use == 0:
return bottle
return recursive(use + bottle % 2, use + head % 4) + bottle
for i in range(1, 100):
print(get(i), recursive(i, i))
"""
如果每瓶啤酒中奖的概率是p
那么期望得到多少瓶啤酒
"""