Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<math.h>
- #include <stdlib.h>
- #include <conio.h>
- using namespace std;
- int n, x[100], nrsol=0;
- void init (int k)
- {
- x[k]=0;
- }
- int existasuccesor(int k)
- {
- return x[k]<2;
- }
- int valid(int k)
- {
- int pi=0, pd=0;
- for(int i=1;i<=k;i++)
- if(x[i]==1)
- pd++;
- else
- pi++;
- if(pi>pd)
- return 0;
- if(pd>n/2)
- return 0;
- return 1;
- }
- int solutie (int k)
- {
- return n==k;
- }
- void tipar()
- {
- nrsol++;
- int i;
- for(i=1;i<=n;i++)
- if(x[i]==1)
- cout<<"(";
- else
- cout<<")";
- cout<<endl;
- }
- void back()
- {
- int k=1;
- init(k);
- while(k)
- {
- while(existasuccesor(k))
- {
- x[k]=x[k]+1;
- if(valid(k))
- if(solutie(k))
- tipar();
- else
- {
- k=k+1;
- init(k);
- }
- }
- k=k-1;
- }
- }
- int main()
- {
- cout<<"Cate paranteze sunt? ";
- cin>>n;
- back();
- cout<<"exista "<<nrsol<<" solutii";
- return 0;
- }
Add Comment
Please, Sign In to add comment