Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3.  
  4. ifstream fin("paranteze.in");
  5. ofstream fout("paranteze.out");
  6.  
  7. int n, a[20], cnti, cntd;
  8.  
  9. void ReadFunction();
  10. void Back(int k);
  11. void WriteFunction();
  12.  
  13. int main()
  14. {
  15. ReadFunction();
  16. Back(1);
  17. }
  18.  
  19. void ReadFunction()
  20. {
  21. fin >> n;
  22. }
  23.  
  24. void Back(int k)
  25. {
  26. if (k > n)
  27. {
  28. WriteFunction();
  29. return;
  30. }
  31.  
  32. for (int i = 1; i <= 2; ++i)
  33. {
  34. if (i == 1 && cntd < n / 2)
  35. {
  36. a[k] = 1;
  37. ++cntd;
  38. Back(k + 1);
  39. --cntd;
  40. }
  41. if (i == 2 && cnti < cntd)
  42. {
  43. a[k] = 2;
  44. ++cnti;
  45. Back(k + 1);
  46. --cnti;
  47. }
  48. }
  49. }
  50.  
  51. void WriteFunction()
  52. {
  53. for (int i = 1; i <= n; ++i)
  54. {
  55. switch(a[i])
  56. {
  57. case 1: fout << "(";
  58. break;
  59. case 2: fout << ")";
  60. }
  61. }
  62. fout << '\n';
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement