93. 递归实现组合型枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int ans[50],vis[50];
int n,m;
void dfs(int u,int cnt)
{
ans[cnt]=u;
vis[u]=1;
if(cnt==m)
{
for(int i=1;i<=m;i++)
printf("%d ",ans[i]);
printf("\n");
vis[u]=0;
return ;
}
for(int i=u+1;i<=n;i++)
{
if(!vis[i])
dfs(i,cnt+1);
}
vis[u]=0;
}
int main()
{
cin >> n>>m;
for (int i = 1; i <= n; i ++ )
dfs(i,1);
return 0;
}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信