Cod sursa(job #2241620)

Utilizator Dragos123Tatar Dragos Vlad Dragos123 Data 16 septembrie 2018 14:59:45
Problema Combinari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <iostream>

std::ifstream fin ("combinari.in");
std::ofstream fout ("combinari.out");

const int MaxN = 20;
int n, k, a[MaxN];

void Write();
bool Ok(int pos);
void Back_Track(int pos);

int main ()
{
    fin >> n >> k;
    Back_Track(1);
    return 0;
}

void Write()
{
    for (int i = 1; i <= k; ++i)
        fout << a[i] << ' ';
    fout << '\n';
}

bool Ok(int pos)
{
    for (int i = 1; i <= pos - 1; ++i)
        if (a[pos] == a[i])
            return false;
    return true;
}

void Back_Track(int pos)
{
    for (int i = a[pos - 1] + 1; i <= n - k + pos; ++i)
    {
        a[pos] = i;
        if (Ok(pos))
        {
            if (pos == k)
                Write();
            else
                Back_Track(pos + 1);
        }
    }
}