Advertisement
Gistrec

Код Грея

Feb 24th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. inline void printVector(vector<bool> &mask) {
  7.     for (int i = 0; i < mask.size(); ++i) {
  8.         cout << (int)mask[i] << " ";
  9.     }
  10.     cout << endl;
  11. }
  12.  
  13. void getCombinations(int n) {
  14.     const unsigned int nIterations = 1 << n; // Количество комбинаций, 2^n
  15.     vector<bool> mask(n); // Текущая комбинация
  16.  
  17.     for (unsigned int i = 1; i < nIterations; i++) {
  18.         // какой по счёту бит инвертируем
  19.         unsigned long position;
  20.         _BitScanForward(&position, i);
  21.         mask[position] = !mask[position];
  22.  
  23.         printVector(mask);
  24.     }
  25. }
  26.  
  27. int main() {
  28.     getCombinations(5);
  29.     system("pause");
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement