Cod sursa(job #1208226)

Utilizator dm1sevenDan Marius dm1seven Data 15 iulie 2014 08:19:21
Problema Combinari Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
using namespace std;
#include <fstream>
#include <exception>

namespace e_017_combs
{
	void back_track_combinari(int k, int N, int K, int* v, ofstream& ofs)
	{
		//if the sequence is full
		if (k == K + 1) {
			//print the solution in file
			for (int i = 1; i <= K; i++) ofs << v[i] << " ";
			ofs << "\n";
		}
		else {
			//fill the element k with all posible values
			for (int i = v[k-1] + 1; i <= N; i++) {
				v[k] = i;
				//fill the rest of the values
				back_track_combinari(k + 1, N, K, v, ofs);
			}
		}
	}
}

//int e_011_permutari()
int main()
{
	using namespace e_017_combs;
	
	int N, K;

	string fin = "combinari.in";
	ifstream ifs(fin);
	//if (!ifs.is_open()) throw std::exception("Input file not found");
	ifs >> N >> K;
	ifs.close();

	int* v = new int[N + 1];
	v[0] = 0;

	ofstream ofs("combinari.out");
	back_track_combinari(1, N, K, v, ofs);
	ofs.close();

	delete[] v;

	return 0;
}