-1

e.g. 12 balls and 3 boxes, how can i add list to all combinations with using all balls. for example n is 12, k is 3:

 - (12,0,0)
 - (0,12,0)
 - (0,0,12)
 - (0,1,11)
 - (0,11,1)
 - (11,1,0)
 - (11,0,1)
 - (10,1,1)...

Python, c#, Java... One of these three languages ​​would be great for me.

EDIT: This is not a homework. This algortihm will be used later for allocation.

  • 1
    you can use `itertools.combinations()` for it, more about it [here](https://docs.python.org/3/library/itertools.html#itertools.combinations) – BoredRyuzaki Aug 11 '21 at 08:45
  • Thanks for comment but combination is not correct answer because i have to use all balls so The sum of the numbers of each combinated element must be 12. – Furkan Meydan Aug 11 '21 at 08:53
  • Ah, sorry about that I misread a bit – BoredRyuzaki Aug 11 '21 at 09:00
  • https://stackoverflow.com/questions/996004/enumeration-of-combinations-of-n-balls-in-a-boxes/41324574 – Dmitry Bychenko Aug 11 '21 at 09:09
  • @Avijeet You are right, just connection is not so simple [link](https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)#Theorem_two_2) – MBo Aug 11 '21 at 09:14

1 Answers1

0
lst, cnt = [], 0
for i in range(13):
    for j in range(13 - i):
        lst.append((i, j, 12 - j - i))
        cnt += 1
print(*lst, sep='\n')
print(f'combinations = {cnt}')
(0, 0, 12)
(0, 1, 11)
(0, 2, 10)
(0, 3, 9)
(0, 4, 8)
(0, 5, 7)
(0, 6, 6)
...
(11, 0, 1)
(11, 1, 0)
(12, 0, 0)
combinations = 91
Алексей Р
  • 7,507
  • 2
  • 7
  • 18