【问题描述】
两种糖果分别有 9 个和 16 个,要全部分给 7 个小朋友,每个小朋友得到
的糖果总数最少为 2 个最多为 5 个,问有多少种不同的分法。
只要有其中一个小朋友在两种方案中分到的糖果不完全相同,这两种方案
就算作不同的方案答案:5067671
def dfs(n: int):
# give candies to the nth child
global candies1, candies2, methods
# candies are not enough
if candies1 < 0 or candies2 < 0:
return
# this child is the last one
if n == 6:
# if leftover candies are meet the requirement
if 2 <= candies1 + candies2 <= 5:
# find a new method
methods += 1
return
for a in range(candies1 + 1):
for b in range(candies2 + 1):
# a ,b is the number of candies given to the n+1th child
if a + b < 2 or a + b > 5:
continue
candies1 -= a
candies2 -= b
dfs(n + 1)
candies1 += a
candies2 += b
methods = 0
candies1, candies2 = 9, 16
dfs(0)
print(methods)
标签:methods,candies,dfs,蓝桥,candies1,candies2,小朋友
From: https://www.cnblogs.com/mengzhuo/p/17601528.html