Cod sursa(job #1513827)

Utilizator jimcarterJim Carter jimcarter Data 30 octombrie 2015 00:02:22
Problema Combinari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <cstdio>
using namespace std;

FILE *f = fopen ( "combinari.in" , "r" ) , *g = fopen ( "combinari.out" , "w" );

const int MAX = 20;
int n , k , i , comb [ MAX ];

bool isGood ( int index )
{
    // 1 <= comb [ index - 1 ] < comb [ index ] <= n
    if ( !( 1 <= comb [ index ] && comb [ index - 1 ] < comb [ index ] && comb [ index ] <= n ) )
        return false;
    return true;
}

void build ( int index )
{
    if ( index > 0 )
        if ( index == k + 1 )
        {
            //print the solution
            for ( i = 1 ; i <= k ; i ++ )
                fprintf ( g , "%d " , comb [ i ] );
            fprintf ( g , "\n");
            build ( k );
        }
        else
        {
            //increase the value of comb [ index ]
            comb [ index ] ++;
            if ( isGood ( index ) )
                build ( index + 1 );
            else
            {
                if ( comb [ index ] > n )
                {
                    comb [ index ] = 0;
                    build ( index - 1 );
                }
                else
                    build ( index );
            }
        }
}

int main()
{
    //read
    fscanf ( f , "%d %d" , &n , &k );

    //set first one
    for ( i = 1 ; i <= k ; comb [ i ] = i , i ++ );

    //build the other ones
    build (k + 1);

    return 0;
}