Cod sursa(job #1877725)

Utilizator cyber_ghSoltan Gheorghe cyber_gh Data 13 februarie 2017 18:12:19
Problema Combinari Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>


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

int N,K;
bool u[30];
int V[30];

void printit(){
	for (int i=1;i<=N;i++) if (V[i]) fout <<V[i]<<" ";
	fout <<"\n";
	
}

bool check(){
	int max1=-1;
	for (int i=1;i<=N;i++) {
		if (V[i]){
			max1=max(max1,V[i]);
			if (max1>V[i]) return 0;
		}
	}
	return 1;
}

void back(int k,int used){
		if (k>N||used==K){
			if (check()) printit();
			return ;
		}
		for (int i=1;i<=N;i++){
			if (!u[i]) {
					u[i]=1;
					V[k]=i;
					back(k+1,used+1);
					u[i]=0;
			}	
		}
}

/*
bool valid(int k){
	for (int i=1;i<k;i++){
		
		if (V[i]>V[k]) return 0;
	}
		return 1;
}


void combinari(int k){
		for(int i=1; i<=N; ++i){
			V[K] = i;
			if(valid(k) && u[i]){
				u[i]=1;
				if(k==K) printit();
				else combinari(k+1);
				u[i]=0;
			}
		}
}
*/
int main(){
	fin >>N>>K;
	back(1,0);
	return 0;
	
}