Advertisement
nikolapetkov824

P03MaxMinElement

Jan 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace MaximumAndMinimumElement
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             Stack<int> stack = new Stack<int>();
  14.  
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string input = Console.ReadLine();
  18.  
  19.                 if (input.Length == 1)
  20.                 {
  21.                     int[] array = input.Split().Select(int.Parse).ToArray();
  22.  
  23.                     int command = array[0];
  24.  
  25.                     while (stack.Any())
  26.                     {
  27.                         if (command == 2)
  28.                         {
  29.                             stack.Pop();
  30.                             break;
  31.                         }
  32.                         else if (command == 3)
  33.                         {
  34.                             Console.WriteLine(stack.Max());
  35.                             break;
  36.                         }
  37.                         else if (command == 4)
  38.                         {
  39.                             Console.WriteLine(stack.Min());
  40.                             break;
  41.                         }
  42.                     }
  43.                 }
  44.                 else
  45.                 {
  46.                     int[] array = input.Split().Select(int.Parse).ToArray();
  47.  
  48.                     int command = array[0];
  49.                     int number = array[1];
  50.  
  51.                     stack.Push(number);
  52.                 }
  53.             }
  54.  
  55.             Console.WriteLine(string.Join(", ", stack));
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement