We use cookies to improve your experience, for authentication and to ensure that we show you advertising that is relevant to you. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on Ideone website. However, if you wish, you can change cookie settings of your browser at any time.
Embed source code on your page
source code clone
download
copy to clipboard
report bug / make suggestion
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
#include <vector>
#include <iostream>
using namespace std;
#define SET_SIZE 5
int set[ ] = { 1 , 2 , 3 , 4 , 5 } ;
vector< vector< int > > all_combinations;
const int tuple_size = 3 ;
void recursive_comb( int step_val, int array_index, std:: vector < int > tuple)
{
if ( step_val == 0 )
{
all_combinations.push_back ( tuple) ; //<==We have the final combination
return ;
}
for ( int i = array_index; i < SET_SIZE; i++ )
{
tuple.push_back ( set[ i] ) ;
recursive_comb( step_val - 1 , i + 1 , tuple) ;
tuple.pop_back ( ) ;
}
return ;
}
void init_combinations( )
{
std:: vector < int > tuple;
tuple.reserve ( tuple_size) ; //avoids needless allocations
recursive_comb( tuple_size, 0 , tuple) ;
}
int main( )
{
init_combinations( ) ;
cout << "Total Combinations: " << all_combinations.size ( ) << endl;
for ( int i= 0 ; i < all_combinations.size ( ) ; i++ )
{
cout << "{" ;
for ( int j= 0 ; j < tuple_size; j++ )
{
cout << all_combinations[ i] [ j] << " " ;
}
cout << "}" << endl;
}
return 0 ;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKI2RlZmluZSBTRVRfU0laRSA1CiAKaW50IHNldFtdID0gezEsIDIsIDMsIDQsIDV9Owp2ZWN0b3I8dmVjdG9yPGludD4gPiBhbGxfY29tYmluYXRpb25zOwpjb25zdCBpbnQgdHVwbGVfc2l6ZSA9IDM7CiAKdm9pZCByZWN1cnNpdmVfY29tYihpbnQgc3RlcF92YWwsIGludCBhcn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ICJ9IiA8PCBlbmRsOwogICAgfQogCiAgICByZXR1cm4gMDsKfQ==
clone
download
copy to clipboard
input / output show all
hide all
upload with new input
result:
Success
time: 0s
memory: 3032 kB
returned value: 0
input: no
output:
Total Combinations: 10
{1 2 3 }
{1 2 4 }
{1 2 5 }
{1 3 4 }
{1 3 5 }
{1 4 5 }
{2 3 4 }
{2 3 5 }
{2 4 5 }
{3 4 5 }
result:
Success
time: 0s
memory: 3032 kB
returned value: 0
output:
Total Combinations: 10
{1 2 3 }
{1 2 4 }
{1 2 5 }
{1 3 4 }
{1 3 5 }
{1 4 5 }
{2 3 4 }
{2 3 5 }
{2 4 5 }
{3 4 5 }
result:
Success
time: 0.01s
memory: 2816 kB
returned value: 0
output:
Total Combinations: 10
{1 2 3 }
{1 2 4 }
{1 2 5 }
{1 3 4 }
{1 3 5 }
{1 4 5 }
{2 3 4 }
{2 3 5 }
{2 4 5 }
{3 4 5 }
result:
Success
time: 0.01s
memory: 2860 kB
returned value: 0
input: no
output:
Total Combinations: 10
{1 2 3 }
{1 2 4 }
{1 2 5 }
{1 3 4 }
{1 3 5 }
{1 4 5 }
{2 3 4 }
{2 3 5 }
{2 4 5 }
{3 4 5 }
Choose your language:
Help us translate Ideone - click here