Cod sursa(job #2756672)

Utilizator george_buzasGeorge Buzas george_buzas Data 2 iunie 2021 12:03:03
Problema Combinari Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
using namespace std;

ifstream fin("combinari.in");
ofstream fout("combinari.out");

short n, k;
char combinations[19];

// current value will be increased at each recursive call, so it ensures that all the combinations will be printed in increasing order
void generate_combinations(short current_value, short pos) {
	if (pos == k + 1) {
		for (int i = 1; i <= k; ++i) {
			fout << combinations[i] << ' ';
		}
		fout << '\n';
	}
	for (int i = 1; i + current_value <= n; ++i) {
		combinations[pos] = '0' + i + current_value;
		generate_combinations(combinations[pos] - '0', pos + 1);
	}
}

int main() {
	fin >> n >> k;
	generate_combinations(0, 1);
	return 0;
}