Advertisement
silvana1303

maximum and minimum element

Sep 20th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace advanced
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int count = int.Parse(Console.ReadLine());
  12.  
  13.             Stack<int> stack = new Stack<int>();
  14.  
  15.             int min = int.MaxValue;
  16.             int max = int.MinValue;
  17.  
  18.             for (int i = 0; i < count; i++)
  19.             {
  20.                 int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  21.  
  22.                 if (input[0] == 1)
  23.                 {
  24.                     stack.Push(input[1]);
  25.                 }
  26.                 if (input[0] == 2)
  27.                 {
  28.                     stack.Pop();
  29.                 }
  30.                 if (input[0] == 3)
  31.                 {
  32.                     if (stack.Any())
  33.                     {
  34.                         foreach (var item in stack)
  35.                         {
  36.                             if (item > max)
  37.                             {
  38.                                 max = item;
  39.                             }
  40.                         }
  41.  
  42.                         Console.WriteLine(max);
  43.                     }  
  44.                 }
  45.                 if (input[0] == 4)
  46.                 {
  47.                     if (stack.Any())
  48.                     {
  49.                         foreach (var item in stack)
  50.                         {
  51.                             if (item < min)
  52.                             {
  53.                                 min = item;
  54.                             }
  55.                         }
  56.  
  57.                         Console.WriteLine(min);
  58.                     }
  59.                 }
  60.             }
  61.  
  62.             Console.WriteLine(string.Join(", ", stack));
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement