码迷,mamicode.com
首页 > 其他好文 > 详细

搜索-子集生成

时间:2020-02-08 17:40:25      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:type   algo   iostream   printf   define   i++   ios   math   set   

技术图片

 

 所以,每个子集对应了一个二进制数:这个二进制数的每个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;
}

 

搜索-子集生成

标签:type   algo   iostream   printf   define   i++   ios   math   set   

原文地址:https://www.cnblogs.com/hznumqf/p/12283807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!