Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int S[100];
  4. int n;
  5. int top;
  6. int init(int S[],int top)
  7. { S[top]=0;}
  8.  
  9. int solutie(int S[],int top)
  10. { return top==n;}
  11.  
  12. int tipar(int S[],int top)
  13. {cout<<endl;
  14. for(int j=1;j<=n;j++)
  15. if(S[j]==1) cout<<")";
  16. else cout<<"(";}
  17.  
  18. int succ(int S[],int top)
  19. {if (S[top]<n) { S[top]++; return 1;}
  20. return 0;}
  21.  
  22. int valid(int S[], int top)
  23. {int pd=0,pi=0;
  24. for(int i=1;i<=top;i++)
  25. if(S[i]==0) pd++;
  26. else pi++;
  27. return pd<=n/2 && pi<=pd;}
  28.  
  29. int bktr(int S[],int top)
  30. {top=1;
  31. while (top>=1)
  32. {
  33. int am,este;
  34. do { am=succ(S,top);
  35. este=valid(S,top);
  36. } while(!( (am&&este) || (!am) ) ) ;
  37. if (am){if (solutie(S,top)) {tipar(S,top);}
  38. else {top++;init(S,top);}
  39. }
  40. else top--;
  41. }
  42. return 0;}
  43.  
  44. int main()
  45. {cout<<"n=";cin>>n;
  46. if(n%2!=0) cout<<"Nu este posibil.";
  47. bktr(S,n);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement