Calculating different ways

Hey guys,

I have to write a program which calculates the number of possibilities how an amount of bottles can be placed in boxes which have different capacities (for example you have 7 bottles and 2 boxes, where box 1 can hold up to 3 bottles and box 2 can hold 5 which gives 2 possibilities). Right now the input works and it tells me the correct result for the example above, but if I try it with e.g. 20 bottles and 5 boxes (capacity of 5 , 8, 9 , 11 and 15), the result is 10, whereas there are a lot more possibilities...
If somebody could have a look at my code and tell me what's wrong, I'd be more than grateful.

Last edited on
Can anybody help me please?
Think about the problem and see if you can discover a way to solve it mathematically. This sounds like a combinations/permutations problem.

If you can't solve it mathematically then I think the code should be recursive. Let's say there are B bottles, X boxes and the first box holds N bottles. You can put 1 bottle in that box, and solve the problem the remaining X-1 boxes and B-1 bottles. The next possibility is 2 bottles in the first box and however many ways you can but B-2 bottles in the remaining X-1 boxes. Continue.

Do you have to put a bottle in each box? In other words, if you have 1 bottle and 2 boxes, is the answer 2, or 0? You'll have to account for this either way.
If you can't solve it mathematically then I think the code should be recursive. Let's say there are B bottles, X boxes and the first box holds N bottles. You can put 1 bottle in that box, and solve the problem the remaining X-1 boxes and B-1 bottles. The next possibility is 2 bottles in the first box and however many ways you can but B-2 bottles in the remaining X-1 boxes. Continue.


Could you elaborate on this a bit more? How would I check that all possibilities for X-1 and B-1 have been tried?

Do you have to put a bottle in each box? In other words, if you have 1 bottle and 2 boxes, is the answer 2, or 0?


The answer would be 0.
Could you elaborate on this a bit more? How would I check that all possibilities for X-1 and B-1 have been tried?
Have you learned about recursion? To solve the smaller problem you would call the function that solves it recursively.
Topic archived. No new replies allowed.