Draymonders / Code-Life

The marathon continues though.
27 stars 3 forks source link

枚举子集 #61

Open Draymonders opened 4 years ago

Draymonders commented 4 years ago

枚举子集

有一个数,比如5, 二进制表示为 101

子集有

101   -> 5
100   -> 4
1     -> 1

code

void enumerate_sub_collection(int x) {
    for (int t=x; t; t=(t-1)&x) {
        cout << t << endl;
    }
}