#include<iostream> #include <vector> using namespace std; vector<int> people; vector<int> combination; void pretty_print(const vector<int>& v) { static int count = 0; cout << "combination no " << (++count) << ": [ "; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << "] " << endl; } void go(int offset, int k) { if (k == 0) { pretty_print(combination); return; } for (int i = offset; i <= people.size() - k; ++i) { combination.push_back(people[i]); go(i+1, k-1); combination.pop_back(); } } int main() { int n = 5, k = 3; for (int i = 0; i < n; ++i) { people.push_back(i+1); } go(0, k); return 0; } /* And here's output for N = 5, K = 3: combination no 1: [ 1 2 3 ] combination no 2: [ 1 2 4 ] combination no 3: [ 1 2 5 ] combination no 4: [ 1 3 4 ] combination no 5: [ 1 3 5 ] combination no 6: [ 1 4 5 ] combination no 7: [ 2 3 4 ] combination no 8: [ 2 3 5 ] combination no 9: [ 2 4 5 ] combination no 10: [ 3 4 5 ] */
Wednesday, 24 June 2015
Combination Idea
Labels:
C++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment