http://ybt.ssoier.cn:8088/problem_show.php?pid=1317
自己能运行,洛谷能运行,她不能运行了? 答案似乎也错了,大佬们帮忙看看。
1317:【例5.2】组合的输出
【题目描述】 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r≤n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r个数。现要求你用递归的方法输出所有组合。 例如n=5,r=3,所有组合为:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
【输入】 一行两个自然数n、r(1<n<21,1≤r≤n)。
【输出】 所有的组合,每一个组合占一行且其中的元素按由小到大的顺序排列,每个元素占三个字符的位置,所有的组合也按字典顺序。
【输入样例】 5 3 【输出样例】 1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
int num=0,a[10001]={0},n,r;
bool b[10001];
int search(int);
int print();
int main()
{
b[1]=0;
cin>>n>>r;
search(1);
//cout<<num;
return 0;
}
int search(int k)
{
int i;
for(i=1;i<=n;i++)
{
if(!b[i])
{
a[k]=i;
b[i]=1;
if(k==r)
print();
else
search(k+1);
b[i]==0;
}
}
}
int print()
{
num++;
for(int i=1;i<=r;i++)
cout<<" "<<a[i];
//cout<<setw(3)<<a[i];
cout<<endl;
}