Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include "Stack.h"
  2.  
  3. int max(int a, int b)
  4. {
  5. if (a > b)
  6. {
  7. return a;
  8. }
  9. return b;
  10. }
  11.  
  12. int MakeEven(Stack<int>& s1, Stack<int>& s2, Stack<int>& s3)
  13. {
  14. while (s1.GetHeight() != s2.GetHeight() || s2.GetHeight() != s3.GetHeight())
  15. {
  16. int height1 = s1.GetHeight();
  17. int height2 = s2.GetHeight();
  18. int height3 = s3.GetHeight();
  19.  
  20. int max_h = max(s1.GetHeight(), max(s2.GetHeight(), s3.GetHeight()));
  21. if (height1 == max_h)
  22. {
  23. s1.pop();
  24. }
  25. else if (height2 == max_h)
  26. {
  27. s2.pop();
  28. }
  29. else s3.pop();
  30. }
  31. return s1.GetHeight();
  32. }
  33.  
  34. int main()
  35. {
  36. int size1, size2, size3;
  37. std::cin >> size1 >> size2 >> size3;
  38.  
  39. Stack<int> s1(size1);
  40. Stack<int> s2(size2);
  41. Stack<int> s3(size3);
  42.  
  43. int input;
  44. for (int i = 0; i < size1; ++i)
  45. {
  46. std::cin >> input;
  47. s1.push(input);
  48. }
  49. s1.reverse();
  50. for (int i = 0; i < size2; ++i)
  51. {
  52. std::cin >> input;
  53. s2.push(input);
  54. }
  55. s2.reverse();
  56. for (int i = 0; i < size3; ++i)
  57. {
  58. std::cin >> input;
  59. s3.push(input);
  60. }
  61. s3.reverse();
  62. std::cout << MakeEven(s1, s2, s3) << std::endl;
  63. return 0;
  64. }
  65.  
  66. //самия стек
  67. #include <iostream>
  68.  
  69. template <class T>
  70. class Stack
  71. {
  72. private:
  73. T * arr;
  74. int count;
  75. int size;
  76. int height;
  77.  
  78. public:
  79. Stack(int);
  80. ~Stack();
  81.  
  82. bool empty();
  83. void push(const T&);
  84. void pop();
  85. void reverse();
  86. T top();
  87. int GetHeight()const;
  88. };
  89.  
  90. template <class T>
  91. Stack<T> ::Stack(int size) :
  92. size(size),
  93. count(0),
  94. height(0)
  95. {
  96. arr = new T[size];
  97. }
  98.  
  99. template <class T>
  100. Stack<T> :: ~Stack()
  101. {
  102. if (arr == nullptr)
  103. {
  104.  
  105. }
  106. else delete[]arr;
  107. }
  108.  
  109. template <class T>
  110. bool Stack<T>::empty()
  111. {
  112. return count == 0;
  113. }
  114.  
  115. template <class T>
  116. void Stack<T> ::push(const T& input)
  117. {
  118. if (count == size)
  119. {
  120. std::cout << "-1\n";
  121. return;
  122. }
  123. arr[count++] = input;
  124. height += input;
  125. }
  126.  
  127. template <class T>
  128. void Stack<T> ::pop()
  129. {
  130. if (empty())
  131. {
  132. std::cout << "Error";
  133. return;
  134. }
  135. height -= arr[count - 1];
  136. --count;
  137. }
  138.  
  139. template <class T>
  140. T Stack<T>::top()
  141. {
  142. return arr[count];
  143. }
  144.  
  145. template <class T>
  146. void Stack<T>::reverse()
  147. {
  148. T *temp = new T[size];
  149. for (int i = 0; i < count; i++)
  150. {
  151. temp[i] = arr[count - i - 1];
  152. }
  153. delete[] arr;
  154. arr = temp;
  155. }
  156.  
  157. template<class T>
  158. int Stack<T>::GetHeight() const
  159. {
  160. return height;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement