题解 | #放苹果#
放苹果
http://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
# 定义苹果数量,盘子数量分别为m n,放置方法为f(m, n)
# 当盘子数量大于m大于苹果数量n时,必然有n - m个盘子空着,此时即f(m, n) = f(m, m)
# 当盘子数量m小于等于苹果数量n时,如果有一个盘子为空,方法为f(m, n - 1);如果每个盘子至少放1个苹果,方法为f(m - n, n)
# 总的方法为f(m, n) = f(m, n - 1) + f(m - n, n)
# 采用递归,n的出口为1,m的出口为0
def method(m, n):
if m < n:
return method(m, m)
elif m == 0 or n == 1:
return 1
else:
return method(m, n - 1) + method(m - n, n)
while True:
try:
a, b = map(int, input().split())
print(method(a, b))
except KeyboardInterrupt:
break