Enumeration enumeration index is a subset of common operation is to enumerate bit vector.
If you want to enumerate a subset of a bit vector, Liu Rujia have very beautiful code, you can refer to.
Tree structure can be described as a combination, this rule must have a deeper meaning,
and may even be a good idea to sum up method (for us far more than the sucker thinking), regret it, talk about it later.
//指数型
#include<bits/stdc++.h>
using namespace std;
const int maxn = 16;
int n;
bool S[maxn];
void dfs(int k)
{
if(k == n+1)
{
for(int i=1; i<=n; ++i) if(S[i]) cout << i << ' ';
putchar('\n');
return;
}
S[k] = 1;
dfs(k + 1);
S[k] = 0;
dfs(k + 1);
}
int main()
{
cin >> n ;
dfs(1);
return 0;
}
//排列型
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10;
int n;
int Q[maxn];
bool used[maxn];
void dfs(int k)
{
if(k == n+1)
{
for(int i=1; i<=n; ++i) cout << Q[i] << ' ' ;
putchar( '\n' );
return;
}
for(int i=1; i<=n; ++i) if(!used[i]) {
used[i] = 1;
Q[k] = i;
dfs(k + 1);
Q[k] = 0;
used[i] = 0;
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
//组合型
#include<bits/stdc++.h>
using namespace std;
const int maxn = 27;
int n, m;
int Q[maxn];
void dfs(int k, int pre)
{
if(k == m + 1)
{
for(int i=1; i<=m; ++i) cout << Q[i] << ' ' ;
putchar('\n');
return;
}
for(int i=pre + 1; i<=n-(m-k); ++i)
{
Q[k] = i;
dfs(k+1, i);
}
}
int main()
{
cin >> n >> m;
dfs(1, 0);
return 0;
}