All possible combination

#include <iostream> #include <math.h> #include <string> using namespace std; int* showcomb(int *arr, int sz, int masknum) { int *temp = new int[sz]; int i = 0; int j = 0; while (masknum || i <= sz) { if (masknum & 1) { temp[j] = arr[i]; j++; } masknum = masknum >> 1; i++; } while (j<sz) { temp[j] = NULL; j++; } return temp; } int main() { int N; cin >> N; int *a = new int[N]; int *combholder = new int[(int)pow(2.0, N)*N]; for (int i = 0; i < N; i++) { cin >> a[i]; } for (int i = 0; i < (int)pow(2.0, N); i++) { int *tempout = showcomb(a, N, i); for (int j = 0; j < N; j++) { combholder[i*N + j] = tempout[j]; if (combholder[i*N + j]) { cout << combholder[i*N + j] << ' '; } } cout << endl; } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.