Print the number of m numbers selected from n.
Method 1: Use recursive thinking.
// Select from the back to the front, select the position I, and then select M-1 in the front I-1. // For example, select 3 from 1 2 3 4 5 // 1. Select 5 and then select 2 from the first 4, in the first four cases, selecting two is a sub-problem and recursion is enough. // 2. If 5 is not included, select 4 directly. Then select 2 from the first three, and then select 2 from the first three as a subproblem. recursion is required. // 3. If it does not contain 4, select 3 directly. Select 2 in the first two, and there are only two. // Vertically, 1, 2, 3 is just a for loop, the initial value is 5, the final value is m // horizontally, the problem is a m-1. Void Combination (int arr [], int nLen, int m, int out [], int outLen) {if (m = 0) {for (int j = 0; j <outLen; j ++) cout <out [j] <"\ t"; cout <endl; return ;}for (int I = nLen; i> = m; -- I) // select a {out [M-1] = arr [I-1] From the back to the front; // select a rear Combination (arr, I-1 m-1, out, outLen); // selected from the previous I-1 for recursion} void PrintCombination (int arr [], int nLen, int m) {if (m> nLen) return; int * out = new int [m]; Combination (arr, nLen, m, out, m); delete [] out ;}
Method 2: Binary combination algorithm:
The idea is to open an array. Its subscript indicates the number of 1 to m, and the value of the array element is 1 to indicate its subscript.
Indicates that the number is selected. If it is 0, it is not selected.
First initialize, set the first n elements of the array to 1, indicating that the first element is combined with the first n.
Scan the "10" combination of array element values from left to right, find the first "10" combination, and change it to the "01" combination, at the same time, move all the "1" on the left to the leftmost end of the array (only the first value changes to 0, otherwise the 1 on the left is originally at the leftmost end, no need to move ).
When the first "1" is moved to the m-n position of the array, that is, when n "1" is all moved to the rightmost end, the last combination is obtained.
For example, find the combination of 3 in five:
1 1 1 0 0 // 1, 2, 3
1 1 0 1 0 // 1, 2, 4
1 0 1 1 0 // 1, 3, 4
0 1 1 1 0 // 2, 3, 4
1 1 0 0 1 // 1, 2, 5
1 0 1 0 1 // 1, 3, 5
0 1 1 0 1 // 2, 3, 5
1 0 0 1 1 // 1, 4, 5
0 1 0 1 1 // 2, 4, 5
0 0 1 1 1 // 3, 4, 5
The Code is as follows:
Void Combine (int arr [], int n, int m) {if (m> n) return; int * pTable = new int [n]; // define the tag buf and set the first m to 1 memset (pTable, 0, sizeof (int) * n); for (int I = 0; I <m; ++ I) pTable [I] = 1; bool bFind = false; do {for (int I = 0; I <n; I ++) // print the current combination {if (pTable [I]) cout <arr [I] <"\ t" ;}cout <endl; bFind = false; for (int I = 0; I <n-1; I ++) {if (pTable [I] = 1 & pTable [I + 1] = 0) {swap (pTable [I], pTable [I + 1]); // change 10 to 01 bFind = true; if (pTable [0] = 0) // if the first digit is 0, 1 before position I is moved to the leftmost. For example, 1 before position I is on the leftmost, no need to move {for (int k = 0, j = 0; k <I; k ++) // O (n) complexity: 1 indicates the first 0 after {if (pTable [k]) {swap (pTable [k], pTable [j]); j ++ ;}}} break ;}}while (bFind); delete [] pTable ;}
The test code is as follows:
int main(){int arr[] = {1,2,3,4,5,6};Combine(arr,sizeof(arr)/sizeof(int),3);//PrintCombination(arr,sizeof(arr)/sizeof(int),3);}
Method 1 result 2 result