Pagini recente » Cod sursa (job #399366) | Cod sursa (job #2537678) | Cod sursa (job #662621) | Cod sursa (job #45672) | Cod sursa (job #1513827)
#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;
}