dfs求全排列相 只需要路径长度为n即可
int n, m;
bool st[N];
int path[N];
int dfs(int x,int state,int start)
{
if(x == m)
{
for(int i = 0; i < n; i ++ )
{
cout << path[i] << " ";
}
cout << endl;
}
for(int i = 1; i <= n; i ++ )
{
if(!(state >> i & 1))
{
path[x] = i;
dfs(x + 1, state | 1 << i, i + 1);
}
}
}
int main()
{
cin >> n >> m;
dfs(0,0,1);
}
n个数选m个全排列 修改路径长度即可
int n, m;
bool st[N];
int path[N];
int dfs(int x,int state,int start)
{
if(x == m)
{
for(int i = 0; i < m; i ++ )
{
cout << path[i] << " ";
}
cout << endl;
}
for(int i = 1; i <= n; i ++ )
{
if(!(state >> i & 1))
{
path[x] = i;
dfs(x + 1, state | 1 << i, i + 1);
}
}
}
int main()
{
cin >> n >> m;
dfs(0,0,1);
}
n个数选m个要求升序 只需要每次从start循环
int n, m;
bool st[26];
int path[26];
int dfs(int x,int state,int start)
{
if(x == m)
{
for(int i = 0; i < m; i ++ )
{
cout << path[i] << " ";
}
cout << endl;
}
for(int i = start; i <= n; i ++ )
{
if(!(state >> i & 1))
{
path[x] = i;
dfs(x + 1, state | 1 << i, i + 1);
}
}
}
int main()
{
cin >> n >> m;
dfs(0,0,1);
}