Pagini recente » Cod sursa (job #3132625) | Cod sursa (job #1260638) | Cod sursa (job #722144) | Cod sursa (job #1754330) | Cod sursa (job #1005216)
#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) {
return a[l-1] < a[l];
}
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;
}
//#include <fstream>
//using namespace std;
//ifstream f("submultimi.in");
//ofstream g("submultimi.out");
//int main()
//{
// int i,j,lim,n;
// f>>n;
// lim=1<<n;
// for(i=1;i<lim;i++)
// {
// for(j=1;j<=n;j++)
// if(i&(1<<(j-1)))
// g<<j<<" ";
// g<<'\n';
// }
// return 0;
//}