Gistrec icon

Код Грея

Gistrec | PRO | 02/25/18 02:54:28 AM UTC | 0 ⭐ | 573 👁️ | Never ⏰ | []
C++ |

700 B

|

None

|

0 👍

/

0 👎

#include <iostream>
#include <vector>
 
using namespace std;
 
inline void printVector(vector<bool> &mask) {
    for (int i = 0; i < mask.size(); ++i) {
        cout << (int)mask[i] << " ";
    }
    cout << endl;
}
 
void getCombinations(int n) {
    const unsigned int nIterations = 1 << n; // Количество комбинаций, 2^n
    vector<bool> mask(n); // Текущая комбинация
 
    for (unsigned int i = 1; i < nIterations; i++) {
        // какой по счёту бит инвертируем
        unsigned long position;
        _BitScanForward(&position, i);
        mask[position] = !mask[position];
 
        printVector(mask);
    }
}
 
int main() {
    getCombinations(5);
    system("pause");
    return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎