-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination.cpp
More file actions
49 lines (43 loc) · 1.29 KB
/
combination.cpp
File metadata and controls
49 lines (43 loc) · 1.29 KB
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
/// generating combination ///
/// implementation in c++ ///
/**
recursion for n = 3 , r = 2;
there will be 3 combination;
123
/ \
/ \
/ \
/ \
1 2
/ \ \
/ \ \
/ \ \
/ \ \
12 13 23
**/
#include <bits/stdc++.h>
using namespace std;
int num[40];
int store[40];
int n , k;
void call_combination (int index , int pos){
if (pos == k){
for (int i = 0; i < k; i++)
cout << store[i];
puts("");
}
else{
int i;
for (i = index; i <= n - k + pos; i++){ /// in every call pos remain still and i increase;
store[pos] = num[i];/// in each call we put a number in store array and it will be fixed in
/// next call (own)
call_combination (i + 1 , pos + 1);/// recursive call to set the number beside the fixed number;
}
}
}
int main (){
scanf ("%d%d" , &n , &k);
for (int i = 0; i < n; i++)
cin >> num[i];
call_combination (0 , 0);
}