按字典序枚举集合元素

  通过位运算可以很快按照字典序的枚举集合的元素.

    

#include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<1<<n;i++){
        for(int j=0;j<n;j++){
            a[n-j-1]=(i>>j)&1;
            }
        for(int j=0;j<n;j++){
            cout<<a[j];
            }
        cout<<endl;
    }
    return 0;
}

下:

原文地址:https://www.cnblogs.com/akrusher/p/5357895.html