Pagini recente » Cod sursa (job #3125555) | Cod sursa (job #2522874) | Cod sursa (job #777090) | Cod sursa (job #898162) | Cod sursa (job #2241620)
#include <fstream>
#include <iostream>
std::ifstream fin ("combinari.in");
std::ofstream fout ("combinari.out");
const int MaxN = 20;
int n, k, a[MaxN];
void Write();
bool Ok(int pos);
void Back_Track(int pos);
int main ()
{
fin >> n >> k;
Back_Track(1);
return 0;
}
void Write()
{
for (int i = 1; i <= k; ++i)
fout << a[i] << ' ';
fout << '\n';
}
bool Ok(int pos)
{
for (int i = 1; i <= pos - 1; ++i)
if (a[pos] == a[i])
return false;
return true;
}
void Back_Track(int pos)
{
for (int i = a[pos - 1] + 1; i <= n - k + pos; ++i)
{
a[pos] = i;
if (Ok(pos))
{
if (pos == k)
Write();
else
Back_Track(pos + 1);
}
}
}