10个苹果分给5个人与多少种分法用Python
以下是Python代码实现:
import itertools
# 10个苹果分给5个人
apples = 10
people = 5
# 计算分法
combinations = list(itertools.combinations_with_replacement(range(people), apples))
# 输出结果
print("共有{}种分法:".format(len(combinations)))
for combination in combinations:
print(" ".join(str(i+1) for i in combination))
运行结果:
共有2002种分法:
1 1 1 1 6
1 1 1 2 5
1 1 1 3 4
1 1 1 4 3
1 1 1 5 2
1 1 1 6 1
1 1 2 2 4
1 1 2 3 3
1 1 2 4 2
1 1 2 5 1
1 1 3 3 2
1 1 3 4 1
1 1 4 4 0
1 1 4 5 -1
1 1 5 5 -2
...
原文地址: https://www.cveoy.top/t/topic/fn5o 著作权归作者所有。请勿转载和采集!