Put 2N balls in M boxes (NM) so that the number of balls in each box is even By: LuoWeifeng2011-3-16 yesterday a MM (old table) asked me a simple algorithm design questions. The question is simple, but the simpler the question, the more challenging it is. Toiterateishuman, torecursedivineL. PeterDeu
Put 2N balls in M boxes (N = M) so that the number of balls in each box is even By: Luo Weifeng 2011-3-16 yesterday one MM (old table) I asked a simple question about algorithm design. The question is simple, but the simpler the question, the more challenging it is. To iterate is human, torecurse pine L. Peter Deu
Place 2N balls in M boxes (N> = M) so that the number of balls in each box is even.
By: Luo Weifeng 2011-3-16
Yesterday, a MM (old deskmate) asked me a simple algorithm design question. The question is simple, but the simpler the question, the more challenging it is.
"To iterate is human, torecurse pine" -- L. Peter Deutsch ("Humans understand iteration, and God understands recursion .")
The problem is not difficult, so it will be solved quickly. The reason for me to look back again is that such a simple problem actually integrates the iterate and recurse methods. The Code is as follows:
Package test;/* place 2N balls in M boxes (N> = M) to make the number of balls in each box even */public class CopyOfBulbSeparation {static final int M = 3; static final int N = 4; static int [] boxQueue = new int [M]; static void SeparationBulb (int length, int N) {if (length = 1) {boxQueue [M-1] = N; for (int I = 0; I <boxQueue. length; I ++) {System. out. print ("/t" + 2 * boxQueue [I] + "/t");} System. out. println (); return ;}for (int I = 0; I <= N; I ++) {boxQueue [M-length] = I; SeparationBulb (length-1, n-I) ;}} public static void main (String [] args) {System. out. println ("M:" + M); System. out. println ("N:" + N); SeparationBulb (M, N );}}
Running result:
M: 3N: 4008026044062080206224242260404422440602620800
The code is easy to say. O (distinct _ distinct) O ~