Pagini recente » Cod sursa (job #2875153) | Cod sursa (job #3183336) | Cod sursa (job #2152847) | Cod sursa (job #2849353) | Cod sursa (job #391134)
Cod sursa(job #391134)
#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;
}