fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. unsigned long long snoob(unsigned long long x)
  5. {
  6. unsigned long long smallest = x & -x;
  7. unsigned long long ripple = x + smallest;
  8. unsigned long long ones = x ^ ripple;
  9. ones = (ones >> 2)/smallest;
  10. return ripple|ones;
  11. }
  12.  
  13. template<typename T, size_t N>
  14. void combines( T(&a)[N], size_t K)
  15. {
  16. static_assert( N < 64 );
  17. if (K == 0 || K > N ) return;
  18. unsigned long long x = (1ull << K) - 1;
  19. for(;x < (1ull << N); x = snoob(x))
  20. {
  21. unsigned long long y = x;
  22. for(size_t i = 0; y; y >>= 1, i++)
  23. if (y&1) cout << a[i] << " ";
  24. cout << endl;
  25. }
  26. }
  27.  
  28. int main(int argc, char * argv[])
  29. {
  30. int a[] = {1,3,5,8,0,7};
  31. combines(a,3);
  32. }
  33.  
Success #stdin #stdout 0.01s 5512KB
stdin
Standard input is empty
stdout
1 3 5 
1 3 8 
1 5 8 
3 5 8 
1 3 0 
1 5 0 
3 5 0 
1 8 0 
3 8 0 
5 8 0 
1 3 7 
1 5 7 
3 5 7 
1 8 7 
3 8 7 
5 8 7 
1 0 7 
3 0 7 
5 0 7 
8 0 7