Can you find all the cases where, if I put 3 balls into 2 boxes
the case is
1 1 1
1 1 2
1 2 2
2 2 2
the columns stand for a ball, the number stands for box, and the row stands for a case.
So we have 4 cases
You should write a function, input m and n, m stand for the number of balls, n stands for the number of boxes, output all the cases.
Example
If m = 3, n = 4 then you should output
1 1 1
1 1 2
1 1 3
1 1 4
1 2 2
1 2 3
1 2 4
1 3 3
1 3 4
1 4 4
2 2 2
2 2 3
2 2 4
2 3 3
2 3 4
2 4 4
3 3 3
3 3 4
3 4 4
4 4 4
Solution Stats
Problem Comments
6 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers67
Suggested Problems
-
Find nearest prime number less than input number
1016 Solvers
-
Create an n-by-n null matrix and fill with ones certain positions
712 Solvers
-
Find last zero for each column
689 Solvers
-
Usage of java.math : N Choose K with unlimited precision
51 Solvers
-
Matrix multiplication across rows
394 Solvers
More from this Author17
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
I don't think you have a complete statement of the problem. Why, in the first example, are arrangements like 1 1 2 excluded? And you only accept the arrangements in a particular order. How do you define the order?
the data ascended by column,when the first column is the same, sort by the second column,...
Thanks - and I realize now that 1 1 2 is not excluded; I just wasn't looking carefully.
The best way to generate such a sequence is using depth-first search or recursion. By prohibiting the usage of IFs, all solutions are inefficient (because we can't cut the tree, and are forced to follow all branches).
For the 3 balls 2 boxes cases, shouldn't the following be part of the answer as well?
1 2 1
2 1 1
2 1 2
2 2 1
Why is 'if' banned, @Binbin Qi? Due to this (stupid, in my opinion) ban, I can not use diff as well. I have put up a temporary change in place, but I suggest you to please fix the problem asap.
And if you are putting up restrictions on the test suite, please mention it in the problem statement.
Also, your own solution and the reference solution do not work.