Pagini recente » Cod sursa (job #269035) | Cod sursa (job #1649362) | Cod sursa (job #656971) | Cod sursa (job #1437582) | Cod sursa (job #2942876)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("combinari.in");
ofstream fout("combinari.out");
const int MAX_LENGTH = 18;
int frequency[MAX_LENGTH + 1];
void generate(int n, int k, int combinationLength, int combination[MAX_LENGTH + 1]) {
if (combinationLength == k + 1) {
for (int index = 1; index < combinationLength; ++index) {
fout << combination[index] << ' ';
}
fout << '\n';
}
for (int number = 1; number <= n; ++number) {
if (frequency[number] == 0) {
frequency[number] = 1;
combination[combinationLength] = number;
generate(n, k, combinationLength + 1, combination);
for (int indexFr = number + 1; indexFr <= n; ++indexFr) {
frequency[indexFr] = 0;
}
}
}
}
int main() {
int n, k;
fin >> n >> k;
int combination[MAX_LENGTH + 1];
generate(n, k, 1, combination);
return 0;
}