第13课作业:子集枚举

潘CSP200十点班

2023-07-17 13:01:28
2023-07-27 13:01:28

信息与公告

子集枚举代码模板

方法1:permutation

int a[100];
……  
for(int k=1; k<=n; k++)   //从n选1 到 n选n 
{
	int b[21]= {0};
	for(int i=1; i<=k; i++) b[i]=1;  // 做一个b数组,放入k个1		
	do
	{			
		for(int j=1; j<=n; j++)   // 输出每个子集 
		{
			if(b[j]==1)
				cout<<a[j]<<" ";				
		}
		cout<<endl;
	}
	while(prev_permutation(b+1,b+n+1));   // 全排列枚举b数组
}