Cod sursa(job #2174793)

Utilizator 24601Dan Ban 24601 Data 16 martie 2018 13:29:42
Problema Combinari Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.14 kb
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#define SIZE 18

static uint8_t sol[SIZE+1], n, k;

static void comb(uint8_t l)
{
    uint8_t i;

    if(l == k) {
        for(i = 0; i < k; i++) {
            printf("%" PRIu8 "%c", sol[i], " \n"[i == k - 1]);
        }
    } else {
        for(i = sol[l - 1] + 1; i <= n; ++i) {
            sol[l] = i;
            comb(l + 1);
        }
    }
}

static void comb_iter(void)
{
    int pos, filled_pos, i;

    pos = 1;
    while (pos > 0) {
        filled_pos = 0;
        while (sol[pos] < n - k + pos && !filled_pos) {
            sol[pos]++;
            filled_pos = 1;
        }

        if (filled_pos) {
            if (pos == k) {
                for (i = 1; i <= k; i++) {
                    printf("%d%c", sol[i], " \n"[i == k]);
                }
            } else {
                pos++;
                sol[pos] = sol[pos - 1];
            }
        } else {
            pos--;
        }
    }
}

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

    scanf("%" SCNu8 "%" SCNu8, &n, &k);

    /*comb(0);*/
    comb_iter();

    return 0;
}