edutedu

paranteze

Dec 11th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include<math.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7. int n, x[100], nrsol=0;
  8. void init (int k)
  9. {
  10. x[k]=0;
  11. }
  12. int existasuccesor(int k)
  13. {
  14. return x[k]<2;
  15. }
  16. int valid(int k)
  17. {
  18. int pi=0, pd=0;
  19. for(int i=1;i<=k;i++)
  20. if(x[i]==1)
  21. pd++;
  22. else
  23. pi++;
  24. if(pi>pd)
  25. return 0;
  26. if(pd>n/2)
  27. return 0;
  28. return 1;
  29. }
  30. int solutie (int k)
  31. {
  32. return n==k;
  33. }
  34.  
  35. void tipar()
  36. {
  37. nrsol++;
  38. int i;
  39. for(i=1;i<=n;i++)
  40. if(x[i]==1)
  41. cout<<"(";
  42. else
  43. cout<<")";
  44. cout<<endl;
  45. }
  46. void back()
  47. {
  48. int k=1;
  49. init(k);
  50. while(k)
  51. {
  52. while(existasuccesor(k))
  53. {
  54. x[k]=x[k]+1;
  55. if(valid(k))
  56. if(solutie(k))
  57. tipar();
  58. else
  59. {
  60. k=k+1;
  61. init(k);
  62. }
  63. }
  64. k=k-1;
  65. }
  66. }
  67. int main()
  68. {
  69. cout<<"Cate paranteze sunt? ";
  70. cin>>n;
  71. back();
  72. cout<<"exista "<<nrsol<<" solutii";
  73. return 0;
  74. }
Add Comment
Please, Sign In to add comment