Place 2N balls in M boxes (N & gt; = M) so that the number of balls in each box is even.

Source: Internet
Author: User
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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.