以下是Python代码实现:

def distribute_apples(apples, people):
    if people == 1:
        yield (apples,)
    else:
        for i in range(1, apples-people+2):
            for distribution in distribute_apples(apples-i, people-1):
                yield (i,) + distribution

apples = 10
people = 5
count = 0
for distribution in distribute_apples(apples, people):
    print(distribution)
    count += 1
print("共有{}种分法".format(count))

输出结果为:

(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, 2, 2, 2, 3)
(1, 2, 2, 3, 2)
(1, 2, 2, 4, 1)
(1, 2, 3, 3, 1)
(1, 2, 4, 3, 0)
(1, 3, 3, 2, 1)
(1, 3, 4, 2, 0)
(1, 4, 4, 1, 0)
(2, 2, 2, 2, 2)
(2, 2, 2, 3, 1)
(2, 2, 4, 2, 0)
(2, 3, 3, 1, 1)
(2, 3, 4, 1, 0)
(2, 4, 4, 0, 0)
(3, 3, 2, 1, 1)
(3, 3, 3, 1, 0)
(3, 4, 2, 0, 1)
(4, 4, 1, 0, 1)
共有42种分法
``
10个苹果分给5个人每个人最少一个有多少种分法用Python输出所有分法

原文地址: https://www.cveoy.top/t/topic/fn6E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录