Pagini recente » Cod sursa (job #2500535) | Cod sursa (job #1007706) | Cod sursa (job #807839) | Cod sursa (job #1038337) | Cod sursa (job #1591013)
#include <iostream>
#include <fstream>
#define nmax 9
using namespace std;
ofstream fout ("combinari.out");
struct solution{
int sol[nmax];
int n;
int k;
};
int v[nmax];
solution read_data (){
ifstream fin ("combinari.in");
solution x;
fin >> x.n >> x.k;
for (auto &i : x.sol)
i = 0;
return x;
}
void print_sol (solution x){
for (int i = 0; i < x.k; i++)
fout << x.sol[i] << " ";
fout << "\n";
}
void backtracking (solution x, int stack_level){
if (stack_level == x.k)
print_sol (x);
if (stack_level == 0)
for (int i = 1; i <= x.n; i++){
x.sol[stack_level] = i;
backtracking (x, stack_level+1);
}
else
for (int i = x.sol[stack_level - 1] + 1; i <= x.n; i++){
x.sol[stack_level] = i;
backtracking (x, stack_level+1);
}
}
int main(){
solution x;
x = read_data ();
backtracking (x, 0);
return 0;
}