Pagini recente » Cod sursa (job #2381728) | Cod sursa (job #1615138) | Cod sursa (job #3357317) | Cod sursa (job #2451927) | Cod sursa (job #1208226)
#include <iostream>
using namespace std;
#include <fstream>
#include <exception>
namespace e_017_combs
{
void back_track_combinari(int k, int N, int K, int* v, ofstream& ofs)
{
//if the sequence is full
if (k == K + 1) {
//print the solution in file
for (int i = 1; i <= K; i++) ofs << v[i] << " ";
ofs << "\n";
}
else {
//fill the element k with all posible values
for (int i = v[k-1] + 1; i <= N; i++) {
v[k] = i;
//fill the rest of the values
back_track_combinari(k + 1, N, K, v, ofs);
}
}
}
}
//int e_011_permutari()
int main()
{
using namespace e_017_combs;
int N, K;
string fin = "combinari.in";
ifstream ifs(fin);
//if (!ifs.is_open()) throw std::exception("Input file not found");
ifs >> N >> K;
ifs.close();
int* v = new int[N + 1];
v[0] = 0;
ofstream ofs("combinari.out");
back_track_combinari(1, N, K, v, ofs);
ofs.close();
delete[] v;
return 0;
}