洛谷 p1157. 递归组合数
原题链接
简单
作者:
咸鱼堆上的猫
,
2024-04-12 15:37:07
,
所有人可见
,
阅读 1
#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int a[N],n,r;
bool st[N];
void dfs(int u,int start)
{
if(u>r) {
for(int i=1;i<=r;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return ;
}
for(int i=start;i<=n;i++)
{
if(!st[i])
{
st[i]=true;
a[u]=i;
dfs(u+1,i+1);//如果为start+1
st[i]=false;
a[u]=0;
}
}
}
int main()
{
cin>>n>>r;
dfs(1,1);
}
//结果
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
start+1
结果
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 3
1 4 5
1 5 3
1 5 4
2 3 4
2 3 5
2 4 3
2 4 5
2 5 3
2 5 4
3 2 4
3 2 5
3 4 5
3 5 4
4 2 3
4 2 5
4 3 5
4 5 3
5 2 3
5 2 4
5 3 4
5 4 3
2 4 5
3 4 5