Untitled
unknown
plain_text
2 years ago
2.5 kB
2
Indexable
Never
//Please do not modify the header files. #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; // Stack class to represent a stack class Stack{ private: int item, i; int front; public: vector<int> arr_stack; // default constructor Stack(){ front = 0; }; int top(){ if (int(arr_stack.size()) == 0){ return -1; } return arr_stack[0]; }; bool isEmpty(){ if (int(arr_stack.size()) == 0){ return true; } return false; }; void push(int value){ arr_stack.push_back(value); return; }; int size(){ return int(arr_stack.size()); }; int pop(){ int removed = arr_stack[0]; arr_stack.erase(arr_stack.begin()); return removed; }; }; int maxEqualSum(Stack s1, Stack s2, Stack s3, int n1, int n2, int n3){ // finding individual sums of each stack int removed; int sum1 = 0, sum2 = 0, sum3 = 0; for (int i = 0; i<n1 ; i++){ sum1 += s1.arr_stack[i]; } for (int i = 0; i<n2 ; i++){ sum1 += s2.arr_stack[i]; } for (int i = 0; i<n3 ; i++){ sum1 += s3.arr_stack[i]; } // finding maximum sum if (sum1 == sum2 == sum3){ return sum1; } else if (sum1 >= sum2 && sum1 >= sum3){ removed = s1.pop(); return maxEqualSum(s1, s2, s3, n1-1, n2, n3); } else if (sum2 >= sum1 && sum2 >= sum3){ removed = s2.pop(); return maxEqualSum(s1, s2, s3, n1, n2-1, n3); } else if (sum3 >= sum2 && sum3 >= sum1){ removed = s3.pop(); return maxEqualSum(s1, s2, s3, n1, n2, n3-1); } return 0; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ cout << "hi\n"; int x; int n1,n2,n3; cin >> n1 >> n2 >> n3; Stack s1, s2, s3; for (int i = 0; i<n1 ; i++){ cin >> x; s1.push(x); } for (int i = 0; i<n2 ; i++){ cin >> x; s2.push(x); } for (int i = 0; i<n3 ; i++){ cin >> x; s3.push(x); } int maxSum; maxSum = maxEqualSum(s1, s2, s3, n1, n2, n3); cout << "max sum is" << maxSum; cout << endl; // cout << "top 1 is " << s1.top(); // cout << "top 2 is " << s2.top(); // cout << "top 3 is " << s3.top(); return 0; }