Advertisement
cska1312

03. Maximum and Minimum Element

May 9th, 2023 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <climits>
  3. #include <stack>
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. int minimum(stack<int> buffer)
  10. {
  11.   int min = INT_MAX;
  12.  
  13.   while(buffer.size())
  14.     {
  15.       int top = buffer.top();
  16.       buffer.pop();
  17.       if(top < min)
  18.         min = top;
  19.     }
  20.   return min;
  21. }
  22. int maximum(stack<int> buffer)
  23. {
  24.   int max = INT_MIN;
  25.  
  26.   while(buffer.size())
  27.     {
  28.       int top = buffer.top();
  29.       buffer.pop();
  30.       if(top > max)
  31.         max = top;
  32.     }
  33.   return max;
  34. }
  35.  
  36. int main()
  37. {
  38.   stack<int> buffer;
  39.   int commands;
  40.   cin >> commands;
  41.  
  42.   while(commands--)
  43.     {
  44.       int command, number;
  45.       cin >> command;
  46.  
  47.       switch(command)
  48.         {
  49.           case 1:
  50.            cin >> number;
  51.            buffer.push(number);
  52.            break;
  53.           case 2:
  54.           if(buffer.size())
  55.             buffer.pop();
  56.           break;
  57.           case 3:
  58.            if(buffer.size())
  59.              cout << maximum(buffer) << endl;
  60.            break;
  61.           case 4:
  62.            if(buffer.size())
  63.             cout << minimum(buffer) << endl;
  64.            break;
  65.         }
  66.     }
  67.  
  68.   while(buffer.size())
  69.     {
  70.       cout << buffer.top();
  71.       buffer.pop();
  72.       if(buffer.size())
  73.         cout << ", ";
  74.     }
  75.   cout << endl;
  76.  
  77.   return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement