fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> people;
  7. vector<int> combination;
  8.  
  9. void pretty_print(const vector<int>& v) {
  10. static int count = 0;
  11. cout << "combination no " << (++count) << ": [ ";
  12. for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; }
  13. cout << "] " << endl;
  14. }
  15.  
  16. void go(int offset, int k) {
  17. if (k == 0) {
  18. pretty_print(combination);
  19. return;
  20. }
  21. for (int i = offset; i <= people.size() - k; ++i) {
  22. combination.push_back(people[i]);
  23. go(i+1, k-1);
  24. combination.pop_back();
  25. }
  26. }
  27.  
  28. int main() {
  29. int n = 4, k = 2;
  30.  
  31. for (int i = 0; i < n; ++i) { people.push_back(i+1); }
  32. go(0, k);
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
combination no 1: [ 1 2 ] 
combination no 2: [ 1 3 ] 
combination no 3: [ 1 4 ] 
combination no 4: [ 2 3 ] 
combination no 5: [ 2 4 ] 
combination no 6: [ 3 4 ]