Cod sursa(job #1591013)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 5 februarie 2016 18:22:04
Problema Combinari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#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;
}