Cod sursa(job #1740432)

Utilizator AplayLazar Laurentiu Aplay Data 11 august 2016 16:32:43
Problema Combinari Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>

#define NMAX 19

using namespace std;

int N, K, answer[NMAX];

void combinari(int level, int maxLevel, int maxValue) {
    if (level == maxLevel) {
        for (int it = 0; it < maxLevel; ++it) {
            printf("%d ", answer[it]);
        }
        printf("\n");
        return;
    }

    for (int it = level + 1; it <= maxValue - maxLevel + level + 1; ++it) {
        if (level > 0 && it != answer[level - 1]) {
            answer[level] = it;
            combinari(level + 1, maxLevel, maxValue);
        } else {
            if (level == 0) {
                answer[level] = it;
                combinari(level + 1, maxLevel, maxValue);
            }
        }
    }
}

int main() {
    freopen("combinari.in", "r", stdin);
    freopen("combinari.out", "w", stdout);

    scanf("%d%d", &N, &K);
    combinari(0, K, N);

    fclose(stdin);
    fclose(stdout);

    return 0;
}