Pagini recente » Cod sursa (job #3129750) | Cod sursa (job #133963) | Cod sursa (job #1273921) | Cod sursa (job #1582080) | Cod sursa (job #1752364)
#include <fstream>
using namespace std;
void Backtrack(int pos, int n, int k, int* solution, ostream &fout);
void PrintSolution(int k, int *solution, ostream &fout);
int main()
{
ifstream fin;
ofstream fout;
fin.open("combinari.in");
fout.open("combinari.out");
int n, k;
fin >> n >> k;
int* solution = new int[k + 1]();
Backtrack(1, n, k, solution, fout);
fin.close();
fout.close();
}
void Backtrack(int pos, int n, int k, int* solution, ostream &fout)
{
if (pos > k)
{
PrintSolution(k, solution, fout);
}
else if (n - solution[pos - 1] < k - pos)
{
return;
}
else
{
for (int i = solution[pos - 1] + 1; i <= n; i++)
{
solution[pos] = i;
Backtrack(pos + 1, n, k, solution, fout);
}
}
}
void PrintSolution(int k, int *solution, ostream &fout)
{
for (int i = 1; i <= k; i++)
{
fout << solution[i] << " ";
}
fout << "\n";
}