我想列举N球的所有可能组合在A框中。
示例:我有8球可以在3框中处理:
box_1 box_2 box_3
case-1 8 0 0
case-2 0 8 0
case-3 0 0 8
case-4 7 1 0
case-5 7 0 1
case-6 6 2 0
...我的第一个问题是,我需要A循环来执行这个任务,但是我希望A、和N成为用户的输入。那么,如何不编写用户可能需要的所有可能的循环呢?
a和N值在2 ~800之间,因此在计算时间上要求很高。如何优化该算法?
如果你用python语言回答我,我将不胜感激。感谢所有的贡献!
发布于 2009-06-15 13:52:43
从python2.6(也可以使用)开始,这很好:
>>> import itertools
>>> boxes = 3
>>> balls = 8
>>> rng = list(range(balls + 1)) * boxes
>>> set(i for i in itertools.permutations(rng, boxes) if sum(i) == balls)
{(0, 1, 7), (3, 1, 4), (0, 4, 4), (1, 0, 7), (4, 0, 4), (3, 0, 5), (1, 2, 5), (1, 7, 0), (0, 8, 0), (1, 4, 3), (6, 0, 2), (4, 3, 1), (3, 3, 2), (0, 5, 3), (5, 3, 0), (5, 1, 2), (2, 4, 2), (4, 4, 0), (3, 2, 3), (7, 1, 0), (5, 2, 1), (0, 6, 2), (6, 1, 1), (2, 2, 4), (1, 1, 6), (0, 2, 6), (7, 0, 1), (2, 1, 5), (0, 0, 8), (2, 0, 6), (2, 6, 0), (5, 0, 3), (2, 5, 1), (1, 6, 1), (8, 0, 0), (4, 1, 3), (6, 2, 0), (3, 5, 0), (0, 3, 5), (4, 2, 2), (1, 3, 4), (0, 7, 1), (1, 5, 2), (2, 3, 3), (3, 4, 1)}发布于 2009-06-15 13:12:49
伪码:
Enumerate(Balls, Boxes)
if Boxes<=0
Error
elseif Boxes=1
Box[1] = Balls
PrintBoxes
else
forall b in 0..Balls
Box[Boxes] = b
Enumerate(Balls-b, Boxes-1)
endfor
endif
end解释
从第一个框开始,如果没有框,抱怨并退出。如果这是要填充的最后一个框,则丢弃所有剩余的球并显示结果。如果有更多的框,首先添加0球,并重复对其他框的过程。然后加1,球2个,直到没有球了。
为了证明该算法的有效性,我给出了一个实数、3个球和2个盒子的例子。
我们有一个名为Box的盒子数组,每个盒子都可以容纳任意数量的球(值)。PrintBoxes打印这些框的当前值。
Box = (0,0)
Enumerate(3, 2)
b=0
Box = (0,0)
Enumerate(3,1)
Box = (3,0)
Print!
b=1
Box = (0,1)
Enumerate(2,1)
Box = (2,1)
Print!
b=2
Box = (0,2)
Enumerate(1,1)
Box = (1,2)
Print!
b=3
Box = (0,3)
Enumerate(0,1)
Box = (0,3)
Print!
Output:
(3,0)
(2,1)
(1,2)
(0,3)
Which are all the combinations.另一个有3个盒子和3个球的例子:
Box = (0,0,0)
Enumerate(3, 3)
b=0
Box = (0,0,0)
Enumerate(3,2)
b=0
Box = (0,0,0)
Enumerate(3,1)
Box = (3,0,0)
b=1
Box = (0,1,0)
Enumerate(2,1)
Box = (2,1,0)
b=2
Box = (0,2,0)
Enumerate(1,1)
Box = (1,2,0)
b=3
Box = (0,3,0)
Enumerate(0,1)
Box = (0,3,0)
b=1
Box = (0,0,1)
Enumerate(2,2)
b=0
Box = (0,0,1)
Enumerate(2,1)
Box = (2,0,1)
b=1
Box = (0,1,1)
Enumerate(1,1)
Box = (1,1,1)
b=2
Box = (0,2,1)
Enumerate(0,1)
Box = (0,2,1)
b=2
Box = (0,0,2)
Enumerate(1,2)
b=0
Box = (0,0,2)
Enumerate(1,1)
Box = (1,0,2)
b=1
Box = (0,1,2)
Enumerate(0,1)
Box = (0,1,2)
b=3
Box = (0,0,3)
Enumerate(0,2)
b=0
Box = (0,0,3)
Enumerate(0,1)
Box = (0,0,3)
Output
(3,0,0)
(2,1,0)
(1,2,0)
(0,3,0)
(2,0,1)
(1,1,1)
(0,2,1)
(1,0,2)
(0,1,2)
(0,0,3)发布于 2009-06-16 01:24:45
有关用python编写的示例,请参阅3.1中的替换。此外,在组合学中,将一个组合替换问题转换为通常的组合-不替换问题是很常见的,这个问题已经内置在2.6迭代工具中。这样做的优点是不生成废弃的元组,比如基于积或置换的解决方案。下面是一个使用标准(n,r)术语的示例,在您的示例中这将是(A,N)。
import itertools, operator
def combinations_with_replacement_counts(n, r):
size = n + r - 1
for indices in itertools.combinations(range(size), n-1):
starts = [0] + [index+1 for index in indices]
stops = indices + (size,)
yield tuple(map(operator.sub, stops, starts))
>>> list(combinations_with_replacement_counts(3, 8))
[(0, 0, 8), (0, 1, 7), (0, 2, 6), (0, 3, 5), (0, 4, 4), (0, 5, 3), (0, 6, 2), (0, 7, 1), (0, 8, 0), (1, 0, 7), (1, 1, 6), (1, 2, 5), (1, 3, 4), (1, 4, 3), (1, 5, 2), (1, 6, 1), (1, 7, 0), (2, 0, 6), (2, 1, 5), (2, 2, 4), (2, 3, 3), (2, 4, 2), (2, 5, 1), (2, 6, 0), (3, 0, 5), (3, 1, 4), (3, 2, 3), (3, 3, 2), (3, 4, 1), (3, 5, 0), (4, 0, 4), (4, 1, 3), (4, 2, 2), (4, 3, 1), (4, 4, 0), (5, 0, 3), (5, 1, 2), (5, 2, 1), (5, 3, 0), (6, 0, 2), (6, 1, 1), (6, 2, 0), (7, 0, 1), (7, 1, 0), (8, 0, 0)]https://stackoverflow.com/questions/996004
复制相似问题