12 tall and different people, lined up in two rows, each row must be from short to high, and the second parallelism corresponds to the first row of people high, ask how many kinds of arrangement? (Java Edition)
Thinking: From the internet to see a half-day people's analysis, finally understand, here to record their own ideas
First use a group of numbers to represent the 12 people, 1,2,3,4,5,6 7,8,9,10,11,12 row into two rows, each row to increment the arrangement, and the corresponding position second row to be greater than the first row, it can be
1 2 3 4 5 6 = The number of the first row is expressed as a number in the second row, and the array sequence is 000000 111111
7 8 9 10 11 12
Or it can be
1 3 5 7 9 all = This array sequence is 010101 010101
2 4 6 8 10 12
So for any sort as long as there is a corresponding sequence of 0 and 1 can be, there is a 0 must correspond to have a 1,
For example, 000111000111 can correspond to the queue is 123789-456 10 11 12, but there is a condition to be satisfied, that is, the sequence is incremented, and the second row is greater than the first row
This problem actually translates in order to get 12 numbers of all possible stacks and out of stack order, 0 for the stack, and 1 for the stack
Solution: First 12 numbers can make up all the combinations of 0 and 1 are 1<<12-1 = 4095, we need to traverse these 4,095 numbers sequentially, for each number of binary representations, we first need to know whether to meet the above conditions, that is, there must be six 1, Then the meeting contains six 1 of the number, divided it into two groups, the first group is the left six bits, the second group is the right six bits, if the second group of each position of the number is greater than the first group, then this number is what we are looking for
1 voidCatalan () {2 intnum;3 int[] Front =New int[6];4 int[] back =New int[6];5 intCounter = 0;6 intI, J, K;7 for(num = 0; num < (1<<12); num++){8 //does it contain six x 1?9 if(Bitcount (num) = = 6){Teni = j = 0; One A for(k = one; K >=0; k--){ - //fill before and after rows - if(Num & (1<<k) = = 0){ the //Scan num from left to right, if you run into a 0, or num& (1<<k) ==0, fill the front row with the corresponding position - //since we're going to scan num from right to left, K is starting at 11 so that 1<<11 can fall to Num's highest bit . -front[i++] = 11-K; -}Else{ + //if it is not 0, that is num& (1<<k)!=0, then fill the back of the corresponding position -Back[j++] = 11-K; + } A } at BooleanOK =true; - //before and after filling, start to determine whether the front row is lower than the condition of the rear - for(intm = 0; M < 6; m++){ - if(Front[m] >Back[m]) { -OK =false; - Break; in } - } to + if(OK) { - logbin (num); thecounter++; * } $ }Panax Notoginseng } - theSystem.out.println ("Total:" +counter); + } A the Private intBitcount (intN) { + intCounter = 0; - while(n>0){ $N &= (n-1); $counter++; - } - returncounter; the } - Wuyi Private voidLogbin (intN) { the System.out.println (integer.tobinarystring (n)); -}
Reference: http://blog.csdn.net/hackbuteer1/article/details/7450250
A queue problem for people of different height