Combined problem and code implementation, combined problem code implementation
Recursive Implementation of combination Problems
Taking the number of m (0 <m <= n) in n as an example, the problem can be divided:
1. First, select the maximum number of n numbers, and then select the number of m-1 in n-1-1 numbers until 1 is selected from the number of n-(m-1.
2. Select a smaller number from n to continue with Step 1 until the maximum number of available numbers is m.
Obviously, the above method is a recursive process. That is to say, the recursive method can be used to obtain all combinations cleanly.
The implementation of recursive methods is as follows:
(1) calculate all the combinations of any m elements from array a [1 .. n.
(2) a [1. n] indicates the candidate set. n is a small candidate set, and n> = m> 0.
(3) B [1. M] is used to store the elements in the current combination (the element subscript is stored here ),
(4) constant M indicates the number of elements in a combination that meets the conditions. M = m. These two parameters are only used to output results.
Code implementation:
- # Include <iostream>
- Using namespace std;
- Void combine (int a [], int n, int m, int B [], const int M)
- {
- For (int I = n; I> = m; I --) // pay attention to the loop range.
- {
- B [M-1] = I-1;
- If (m> 1)
- Combine (a, I-1 m-1, B, M );
- Else // m = 1, output a combination
- {
- For (int j = M-1; j> = 0; j --)
- Cout <a [B [j] <"";
- Cout <endl;
- }
- }
- }
- Int main ()
- {
- Const int N = 4;
- Int a [N];
- For (int I = 0; I <N; I ++)
- A [I] = I + 1;
- For (int M = 1; M <= 4; M ++)
- {
- // Recursive Method
- Int B [M];
- Combine (a, N, M, B, M );
- }
- Return 0;
- }
Running result
Question about JAVA code arrangement and combination
Package test;
Public class tt {
/**
* @ Param args
*/
Public static void main (String [] args ){
String [] arr = {"a", "B", "c", "d", "e", "f", "g", "h ", "I", "j", "k", "l", "m", "n", "o", "p", "q", "r ", "s", "t", "u", "v", "w", "x", "y", "z"
, "0", "1", "2", "3", "4", "5", "6", "7", "8 ", "9 "};
Int h = 0;
For (int I = 0; I <arr. length; I ++)
{
For (int j = 0; j <arr. length; j ++)
{
For (int k = 0; k <arr. length; k ++)
{
System. out. print (arr [I] + arr [j] + arr [k] + "");
H ++;
}
System. out. println ();
}
}
}
}
I forgot about unicode encoding, and I wrote it directly. Founder implemented it. Haha
For the code of the compound arrangement and combination !!
No one can solve the problem.