Java code implementation: 12 tall and different people, lined up in two rows, each row must be from low to high, and the second parallelism corresponds to the first row of the person high, ask how many kinds of arrangement?

Source: Internet
Author: User

This topic refers to other people's ideas, 2 ways of solving problems.

1.

1 /**2 * Implemented in Java code: 12 tall and different people, lined up in two rows, each row must be from low to high, and the second parallelism corresponding to the first row of people high, ask how many kinds of arrangement?3 * State tree Mode solution4 * In the form of a state spanning tree, 12 people are numbered from low to high first ,5 * starting from (1; 2), adding 3 and 4 (1,3; 2,4) and (3,4),6 * Then add 5 and 6, respectively, from the previous two states, you can generate 5 states, that is, 6 people have 5 kinds of arrangement7  * @authoramos-s8 * @Time: March 10, 2017 pm 6:18:379  * @version1.0Ten  */ One  Public classLineupproblem { A  -     Private intCount = 0; -     Private intTotal = 0; the     Privatestack<integer> Place =NewStack<integer>(); -  -      PublicLineupproblem (intTotal ) { -          This. Total =Total ; +     } -  +     Private voidLineorder (intOrderintLevel ) { A Place.push (order); at         if(Level >= TOTAL/2) { -count++; -              for(Integer integer:place) { - System.out.print (integer); -             } - System.out.println (); in Place.pop (); -             return; to         } +          -          for(inti = order + 1; I < 2 * (level + 1); ++i) { the Lineorder (I, level); *         } $ Place.pop ();Panax Notoginseng     } -      the  +      Public voidLineorder () { ALineorder (1, 1); theSystem.out.println ("First row a total" + count + "sort arrangement"); +     } -  $      Public Static voidMain (string[] args) { $Lineupproblem line =NewLineupproblem (12); - Line.lineorder (); -     } the}

2.

1 /**2 * Implemented in Java code: 12 tall and different people, lined up in two rows, each row must be from low to high, and the second parallelism corresponding to the first row of people high, ask how many kinds of arrangement?3 * 1. The current number is {0,1,2,3,4,5}, after the + + operation, the number becomes {0,1,2,3,4,6}, because 6 is less than and equal to the maximum value of position 5 2n = 10, so this combination is a matching requirement. 4     5 * 2. Also as the current number is {0,1,2,3,4,10}, + + operation, the number becomes {0,1,2,3,4,11}, because 11 is greater than the maximum value of position 5 2n = ten,6 so to carry, so that the position of 4 + +, and then check the position 4 + + is 5, less than and equal to the maximum position of 4 2n = 8, this position is eligible,7 and the number of positions 5 to set "0", this 0 can not be again from the starting value of the range, but rather equal to the number of position 4 +1. Because the number starting from the starting value is definitely less than the value of position 4. 8     9 * 3. And so on, if the current number is {0,1,4,6,8,10},++ operation, need to carry, step by step forward bit, the final carry position 1 number, the number becomes {0,2,5,7,9,11},Ten Then to place the position 1 after the position of the number "0", equal to the number of the previous position of +1, so the last number is {0,2,3,4,5,6}. One      A * 4. Check if this combination of numbers meets the criteria is to check if the last number is less than and equal to the maximum value of its position.  -  *  -  * @authoramos-s the * @Time: March 10, 2017 pm 6:18:37 -  * @version1.0 -  */ -  Public classLineUpProblem2 { +  -     Private intCount = 0; +     Private intTotal = 0; A  at      PublicLINEUPPROBLEM2 (intTotal ) { -          This. Total =Total ; -     } -  -     Private voidLineorder () { -         // the in         //start writing code to find out how many kinds of permutations -         if(total% 2! = 0) to             return; +         //Initialize the array and complete the first arrangement -         int[] data =New int[TOTAL/2]; the          for(inti = 1; i < data.length; ++i) { *Data[i] =i; $         }Panax Notoginsengcount++; -         //where to save an array of operations the         inti = Data.length-1; +         intAllcount = 0; A          while(I >= 0) { the              +data[i]++;//number of operations to be +1 -             if(Data[i] <= i * 2) { $                 //initializes the number following its number $                  for(intj = i + 1; J < Data.length; ++j) { -DATA[J] = data[j-1] + 1;//ensure that it is the previous +1, minimum number combination -                 } the}Else { -                 //This position has reached the maximum value, and the previous bit is manipulatedWuyii--; the                 Continue; -             } Wu             //Check that the last digit is less than and equal to the maximum value of its position, and if so, this combination of numbers meets the requirements -System.out.println (data[data.length-1]); About             if(Data[data.length-1] <= data[data.length-1] * 2) { $count++;//arrangement Mode +1 -                 //Place the position of the number of the + + operations back to the last position.  -i = Data.length-1; -}Else { A                 //if it is greater than the maximum value of its position, the description has been traversed, exceeding the maximum value for this number combination.  +                  Break; the             } -              $         } the          the          the         //End_code the     } -  in      Public voidLineorder () { theLineorder (); theSystem.out.println ("First row a total" + count + "sort arrangement"); About     } the  the      Public Static voidMain (string[] args) { theLINEUPPROBLEM2 line =NewLINEUPPROBLEM2 (12); + Line.lineorder (); -     } the}

Java code implementation: 12 tall and different people, lined up in two rows, each row must be from low to high, and the second parallelism corresponds to the first row of the person high, ask how many kinds of arrangement?

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.