Cod sursa(job #1471405)

Utilizator AndraSerbanSerban Andra Costinela AndraSerban Data 13 august 2015 19:01:50
Problema Combinari Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <iostream>
#include <fstream>

using namespace std;

int x[20],n,k,p,ok;
void afisare()
{
    ofstream g("combinari.out");
	int i;
	for(i=1;i<=p;i++)
		g<<x[i]<<" ";
	g<<endl;
	g.close();
}

void cont(int k, int &ok)
{
	ok=1;
	if((k>1) && (x[k]<=x[k-1]))
		ok=0;
}
int succesor(int k)
{
	if(x[k]<n-p+k)
	{
		x[k]=x[k]+1;
		return 1;
	} else return 0;
}
int main()
{
    ifstream f("combinari.in");
	 f>>n;
	 f>>p;
	k=1;
	x[k]=0;
	while(k>0)
	{
		ok=0;
		while(!ok && succesor(k))
			cont(k,ok);
		if(!ok)
			k--;
		else if(k==p) afisare();
		else
		{
			k++;
			x[k]=0;
		}
	}
	f.close();
}