Pagini recente » Cod sursa (job #1334328) | Cod sursa (job #581241) | Cod sursa (job #1460621) | Cod sursa (job #1432670) | Cod sursa (job #2334762)
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
void print(const int, const int, std::ostream &);
int main()
{
std::ifstream fin("combinari.in");
std::ofstream fout("combinari.out");
int n, k;
fin >> n >> k;
print(n, k, fout);
return 0;
}
void backtracking(
const int level,
const int N,
const int K,
const int previous,
std::vector<int> &V,
std::ostream &out)
{
if (level == K)
{
std::copy(V.begin(), V.end(), std::ostream_iterator<int>(out, " "));
out << '\n';
return;
}
for (int i = previous + 1; i <= N - K + 1 + level; ++i)
{
V[level] = i;
backtracking(level + 1, N, K, i, V, out);
}
}
void print(const int N, const int K, std::ostream &out)
{
std::vector<int> V(K, 0);
backtracking(0, N, K, 0, V, out);
}