(1) Recursion
# Include <stdio. h>
# Include <stdlib. h>
Int N, R;
Int COUNT = 0;
Void printr (int A [])
{
Count ++;
For (Int J = R-1; j> = 0; j --)
Printf ("% d", a [J]);
Printf ("/N ");
}
Void comb (int A [], int M, int K)
{
For (INT I = m; I> = K; I --)
{
A [k-1] = I;
If (k> 1)
Comb (A, I-1, k-1 );
Else
Printr ();
}
}
Int main ()
{
Printf ("N (total), R (FETCH ):");
Scanf ("% d", & N, & R );
Int * A = (int *) malloc (sizeof (INT) * n );
Comb (A, N, R );
Printf ("Total number: % d", count );
Printf ("/N ");
Return 0;
}
(2) binary method
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
"01" combination, and move all "1" on the left to the leftmost end of the array.
When the first "1" is moved to the M-N position of the array, that is, when n "1" are all moved to the rightmost end
To the last combination.
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 algorithm code is as follows:
# Include <iostream>
Using namespace STD;
Int main ()
{
Int N, R;
Cout <"input N (total), R (choose):" <Endl;
Cin> N> r;
Int * A = new int [N];
For (INT I = 0; I <n; I ++)
{
If (I <R)
A [I] = 1;
Else
A [I] = 0;
}
Char c = 'y ';
Int COUNT = 0;
While (C = 'y ')
{
C = 'n ';
For (INT I = 0; I <n; I ++)
{
If (A [I] = 1)
Cout <I + 1 <"";
If (I = N-1)
Count ++;
}
Cout <Endl;
For (INT I = 0; I <n-1; I ++)
{
If (A [I] = 1 & A [I + 1] = 0 & A [0]! = 0)
{
A [I] = 0;
A [I + 1] = 1;
C = 'y ';
Break;
}
Else if (a [I] = 1 & A [I + 1] = 0 & A [0] = 0)
{
A [I] = 0;
A [I + 1] = 1;
C = 'y ';
Int K = 0; // K is used to record the number of 1 before the '10' string
For (Int J = 0; j <I; j ++)
If (A [J] = 1)
K ++;
// Place all the 1 values before the '10' string to the beginning, and the corresponding 0 values follow
For (Int J = 0; j <I; j ++)
{
If (j <K)
A [J] = 1;
Else
A [J] = 0;
}
Break;
}
}
}
Cout <"total number is:" <count <Endl;
Delete;
A = NULL;
Return 0;
}