You are on page 1of 2

#include<fstream.h> #include<stdlib.h> ofstream f("parant.

txt"); int x[50],k,valid,n,m; void citire() {cout<<"n=";cin>>n; } void posibil(int k,int &valid) { valid=1; int pi=0; int pd=0; for (int i=1;i<=k;i++) if(x[i]==0)pd++; else pi++; if(pd>n/2||pi>n/2)valid=0 if(k==1&&x[k]==1)valid=0; /*prima paranteza din sir nu poate fi paranteza inchisa*/ if(pi>pd)valid=0;/*nu pot sa apara in sir mai multe paranteze inchise decat deschise*/ } int solutie(int k) {/*Avem solutie cand avem in sir n/2 paranteze inchise si n/2 paranteze deschise*/ int pd=0; int pi=0; for (int i=1;i<=k;i++) if(x[i]==0)pd++; else pi++; if(pd==n/2&&pi==n/2)return 1; else return 0; } void afisare(int k) {/*afisam solutia*/ for(int i=1;i<=k;i++) if(x[i]==0) f<<"("; else f<<")"; f<<endl; } void back() { k=1; x[k]=-1;/*initializam x[k] cu valoarea minima-1*/ while(k>0) { valid=0; while(!valid && x[k]<1) {

valid). else { k++. posibil(k. else if(solutie(k)) afisare(k). } if(!valid)k--./*initializam elementul curent din solutie cu valoarea minima-1*/ } } } void main() { citire().close().x[k]=x[k]+1. x[k]=-1. } . back(). f.