-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsets_recursive.cpp
66 lines (57 loc) · 1.82 KB
/
subsets_recursive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Give a set of {1, 2 .., n} integers, this code
// works out all the possible subsets of size k.
// RECURSIVE METHOD
// Compile with: g++ subsets_recursive.cpp -o subsets_rx -lm -O
// on Linux or Unix run with command ./subsets_rx
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <iomanip>
#include<limits>
using namespace std;
long counter = 0;
int child = 0;
void subset_recurse(long nk, long kk, long* &S, long k);
int main (void)
{
long n = 0, k = 5;
while ( true )
{
cout << "This program works out the subsets of size k of (1,2, ..., n)\n";
cout << "Enter a value for n: "; cin >> n;
cout << "Enter a value for k: "; cin >> k;
if( isnan(n) || isnan(k) || k > n)
{
cout << "Input error!\nHit Enter to continue ...";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else
break;
}
long *pS = new long[k];
subset_recurse( n, k, pS, k);
cout << "\nThe number of subsets are: " << counter << endl;
cout << n << " Choose " << k << " is " << counter << endl;
delete[] pS;
return(0);
}
void subset_recurse(long nk, long kk, long* &S, long k)
{
if( kk == 1) //Must print out all the subsets
{
for( ; nk > 0 ; nk-- )
{
cout << " {" << (S[0] = nk) ;
for( int j = 1; j < k ; j++ )
cout << ", " << S[j];
cout << "}" << " subset number=" << setw(3) << ++counter << endl;
}
}
else
for( ; nk > kk - 1; nk -- ) //Recursive call
{
S[kk-1] = nk;
subset_recurse(nk -1, kk - 1, S, k);
}
}