Pagini recente » Cod sursa (job #456485) | Cod sursa (job #1618506) | Cod sursa (job #1614828) | Cod sursa (job #782502) | Cod sursa (job #1003566)
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("combinari.in");
ofstream g("combinari.out");
int n, k, a[19];
void init (int l) {
a[l] = l-1;
}
bool succesor (int l) {
a[l]++;
return a[l] < n-(k-l)+1;
}
bool valid (int l) {
bool ok = true;
int i;
for (i = 1; i <= l-1; i++)
if (a[i] >= a[l])
ok = false;
return ok;
}
bool sol (int l) {
return l == k; // Am completat toate cele n locuri?
}
void tip () {
int i;
for (i = 1; i <= k; i++)
g << a[i] << ' ';
g << endl;
}
void bt (int l) {
init (l); // initializare
while (succesor(l)) // cat timp avem un succesor
if (valid(l)) // Sunt indeplinite conditiile?
if (sol(l)) // Avem o solutie completa?
tip(); // Prezentam solutia.
else // Nu?
bt(l+1); // Trecem la nivelul urmator.
}
int main()
{
f>>n>>k;
bt(1);
return 0;
}