基于集合的子集与集合的全排列的相关问题
时间:2023-09-23 07:03:46
#include<iostream>
using namespace std;
//非递归求解所有的子集
void fun(int a[] , int n)
{
int i = 0 , j ;
while(i < (1<<n)) //2的n次方
{
for(j = 0 ; j < n ; j ++)
{
if(i&(1<<j))
{
cout<<a[j]<<"\t";
}
}
cout<<endl;
i++;
}
}
//递归求解所有的子集
void print(int a[],bool flag[],int k,int length)
{
if(k>=length)
{
for(int i=0;i<length;i++)
{
if(flag[i]) cout<<a[i]<<"\t";
}
cout<<endl;
return ;
}
for(int j=0;j<2;j++)
{
if(j==0)
{
flag[k]=true;
print(a,flag,k+1,length);
flag[k]=false;
}
else
{
flag[k]=false;
print(a,flag,k+1,length);
flag[k]=true;
}
}
}
//集合的全排列算法
void Perm(int list[], int k ,int length)
{
if(k>=length)
{
for(int i=0;i<length;i++)
cout<<list[i]<<"\t";
cout<<endl;
return ;
}
for( int j=k;j<length;j++)
{
swap(list[k],list[j]);
Perm(list,k+1,length);
swap(list[k],list[j]);
}
}
int main()
{
int list[]={1,2,3,4};
bool flag[]={false,false,false,false};
fun(list,4);
print(list,flag,0,4);
Perm(list,0,4);
return 0;
}
标签:集合的子集,集合的全排列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Flutter禁止手机横屏的简单实现方法
2022-12-06 19:56:34
Android全屏设置的方法总结
2021-07-11 04:09:35
Java 数据结构与算法系列精讲之单向链表
2023-07-10 08:22:12
![](https://img.aspxhome.com/file/2023/1/63441_0s.gif)
.net后台获取html控件值的2种方法
2023-01-07 09:12:30
slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换
2023-08-08 13:00:41
![](https://img.aspxhome.com/file/2023/8/57928_0s.png)
Java单例模式的应用示例
2023-08-22 06:54:03
![](https://img.aspxhome.com/file/2023/4/88754_0s.png)
Android开发实现标题随scrollview滑动变色的方法详解
2022-10-04 07:52:24
![](https://img.aspxhome.com/file/2023/6/139566_0s.png)
解决SpringBoot自定义拦截器和跨域配置冲突的问题
2022-01-31 04:59:29
SpringBoot快速搭建实现三步骤解析
2021-06-12 22:33:24
![](https://img.aspxhome.com/file/2023/1/122321_0s.png)
java递归算法实例分析
2022-02-09 07:59:17
![](https://img.aspxhome.com/file/2023/0/68170_0s.png)
判断List和Map是否相等并合并List中相同的Map
2022-12-29 02:11:10
Flutter加载图片流程之ImageProvider源码示例解析
2021-07-19 05:05:19
Android手机获取root权限并实现关机重启功能的方法
2022-02-19 13:56:49
理解Java中的内存泄露及解决方法示例
2023-04-24 23:19:51
![](https://img.aspxhome.com/file/2023/7/78987_0s.png)
c#实现的操作oracle通用类
2022-01-31 15:08:37
Java基础之关键字final详解
2022-01-28 00:55:31
从"Show tabs"了解Android Input系统
2023-12-21 05:20:49
![](https://img.aspxhome.com/file/2023/9/138299_0s.png)
用JAVA实现单链表,检测字符串是否是回文串
2021-07-20 07:07:45
带你了解Java数据结构和算法之数组
2022-09-29 21:18:33
![](https://img.aspxhome.com/file/2023/7/83087_0s.png)
Java的接口调用时的权限验证功能的实现
2023-08-09 11:15:06
![](https://img.aspxhome.com/file/2023/0/70560_0s.png)