zoukankan      html  css  js  c++  java
  • 搜索子集生成

     所以,每个子集对应了一个二进制数:这个二进制数的每个1都代表了一个元素,也因此所以子集的数量是2n

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<set>
    #include<map>
    #include<cmath>
    const double PI = acos(-1.0);
    #define INF 0x3f3f3f3f
    typedef long long ll;
    using namespace std;
    
    const int maxn = 25;
    int a[maxn];
    void print_subset(int n) {
        for (int i = 0; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if (i & (1 << j)) {
                    printf("%d ", a[j]);
                }//从i的最低为开始逐个检查每一位,如果是1,打印
            }
            printf("\n");
        }
    }
    int main() {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) scanf("%d", &a[i]);
        print_subset(n);
        return 0;
    }
  • 相关阅读:
    js
    原型、原型链、闭包、继承
    js6.22
    js
    js
    在浏览器窗口上添加一个遮罩层
    git使用笔记
    前端开发面试题
    Web Worker
    js实现图片预加载
  • 原文地址:https://www.cnblogs.com/hznumqf/p/12283807.html
Copyright © 2011-2022 走看看