跳到正文

个人记录

枚举子集

zzr8178541919约 1 分钟
原文
文章信息与版本

文章信息

发布
2020年11月21日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 1 分钟

版本

  1. v1当前

非常漂亮的枚举子集的写法

有短又快!

C++
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#define reg register
#define int long long 
using namespace std;
const int maxn=1e5+5;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch=='-')
		f=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
signed main()
{
	while(true)
	{
		int S;//输入集合
		cin>>S;
		for(int x=S;x;x=(x-1) & S)//x为子集
		{
			cout<<x<<" ";
		}
		cout<<endl;
	}
	return 0;
}
/*
3 3 5 4
1 4 6
*/
原文