当前位置:网站首页>Recursive implementation of exponential, permutation and combination enumerations
Recursive implementation of exponential, permutation and combination enumerations
2022-06-12 05:45:00 【Python's path to becoming a God】
Exponential type
#include <iostream>
using namespace std;
const int N = 15;
int st[N],num[N];
int n;
void dfs(int u)
{
if(u > n)
{
for(int i = 1;i <= n ;i ++)
{
if(st[i] == 1)
cout <<i<<" ";
}
cout <<endl;
}
else
{
st[u] = 2;
dfs(u+1);
//st[u] = 0;
st[u] = 1;
dfs(u+1);
//st[u] =0 ;
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
Permutation type
#include <iostream>
using namespace std;
const int N = 12;
int n;
bool st[N];
int num[N];
void dfs(int u)
{
if(u > n)
{
for(int i = 1;i <= n;i ++)
{
cout <<num[i]<<" ";
}
cout <<endl;
}
else
{
for(int i = 1;i <= n;i ++)
{
if(st[i] == false)
{
st[i] = true;
num[u] = i;
dfs(u+1);
st[i] = false;
}
}
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
Combination
#include <iostream>
using namespace std;
const int N = 110;
bool st[N];
int num[N];
int n,m;
void dfs(int u,int start)
{
if(u > m)
{
for(int i = 1;i <= m;i ++)
{
cout << num[i]<<" ";
}
cout <<endl;
}
else
{
for(int i = start ;i <= n;i ++) //for Loop here is to arrange enumerations differently
{
if(st[i] == false)
{
st[i] = true;
num[u] = i;
dfs(u+1,i+1);
st[i] = false;
}
}
}
}
int main()
{
cin >> n>> m;
dfs(1,1);
return 0;
}
边栏推荐
猜你喜欢

Introduction to redis high availability

Review notes of naturallanguageprocessing based on deep learning

16. Somme des trois plus proches

Nature | 给全球的新冠伤亡算一笔账

Golang idea configures the agent to improve the speed of packages downloaded by go get

基于tensorflow的校园绿植识别

What is the project advance payment

Halcon 3D 1 读取3d数据

How Wireshark decrypts WiFi data packets

Webrtc AEC process analysis
随机推荐
Optipng can optimize the compressed PNG picture file format
yolov5
Filter的注解配置
38. appearance series
FPGA语法的细节
论文阅读_图神经网络GIN
Towards End-to-End Lane Detection: an Instance SegmentationApproach
项目管理与统筹
Reverse linked list
Lesson 5: data warehouse construction (III)
What is the project advance payment
Greenplum [question 05] Greenplum streaming server custom client problem handling (increasing)
Conversion of Halcon 3D depth map to 3D image
利用jieba库进行词频统计
Nature | 给全球的新冠伤亡算一笔账
What is the lszrz protocol used at ordinary times? Talk about xmodem/ymodem/zmodem
Memory model, reference and function supplement of program
Webrtc AEC process analysis
IO system - code example
TCP and UDP introduction