Pagini recente » Cod sursa (job #2327951) | Cod sursa (job #1664602) | Cod sursa (job #1182472) | Cod sursa (job #3172184) | Cod sursa (job #2093991)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
typedef unsigned int uint;
void backtrack(uint *stiva, uint nivel, uint a, uint b, FILE *out);
void tipar(uint *stiva, uint nivel, FILE *out);
bool valid(uint *stiva, uint nivel, uint last);
int main(void)
{
FILE *in = fopen("combinari.in", "r");
FILE *out = fopen("combinari.out", "w");
if(in != NULL && out != NULL)
{
uint a, b;
fscanf(in, "%u%*c%u%*c", &a, &b);
uint stiva[20];
backtrack(stiva, 0, a, b, out);
fclose(in);
fclose(out);
}
else
{
printf("Error\n");
}
return 0;
}
bool valid(uint *stiva, uint nivel, uint last)
{
uint i = 0;
for(; i < nivel; i++)
{
if(stiva[i] == last)
{
return false;
}
}
return true;
}
void tipar(uint *stiva, uint nivel, FILE *out)
{
uint i = 0;
for(; i <= nivel; i++)
{
fprintf(out, "%u ", stiva[i]);
}
fprintf(out, "\n");
}
void backtrack(uint *stiva, uint nivel, uint a, uint b, FILE *out)
{
uint i = (nivel > 0) ? stiva[nivel - 1] + 1 : 1;
for(; i <= a; i++)
{
stiva[nivel] = i;
if(valid(stiva, nivel, i))
{
if(nivel == (b - 1))
{
tipar(stiva, nivel, out);
}
else
{
backtrack(stiva, nivel + 1, a, b, out);
}
}
}
}