Cod sursa(job #391134)

Utilizator tomescu_alinTomescu Alin tomescu_alin Data 5 februarie 2010 09:55:31
Problema Combinari Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ulonglong;

#define NDEBUG

#ifdef NDEBUG
#define dbg	0 && (*((ostream *) 0))
#else
#define dbg clog
#endif

void outputCombinations(std::ofstream& fout, uint n, uint k)
{
	std::vector<uint> stack;
	
	stack.push_back(1);
	while(stack.size() < k)
	{
			stack.push_back(stack.back() + 1);
	}
	
	std::copy(stack.begin(), stack.end(), std::ostream_iterator<uint>(fout, " "));
	fout << std::endl;
	
	do
	{
		if(stack.back() < n - (k - stack.size()))
		{
			stack.back()++;
			
			while(stack.size() < k)
			{
				stack.push_back(stack.back() + 1);
			}
			
			std::copy(stack.begin(), stack.end(), std::ostream_iterator<uint>(fout, " "));
			fout << std::endl;
		}
		else
		{
			stack.pop_back();
		}
		
	} while(!stack.empty());
}

int main(int argc, char * argv[]) 
{
	
	const char * inFile = "combinari.in";
	const char * outFile = "combinari.out";
	
	ifstream fin(inFile);
	ofstream fout(outFile);
	
	#ifndef NDEBUG
		if(!fin || !fout) 
		{
			cerr << "Error opening one of \"" << inFile << "\" or \"" << outFile << "\"" << endl;
			return -1;
		}
	#endif
	
	uint n, k;
	fin >> n >> k;
	
	outputCombinations(fout, n, k);
	
	fout.close();
	fin.close();
	
	return 0;
}