Advertisement
silvana1303

basic stack operations

Sep 20th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 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[] numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             int numberPop = numbers[1];
  13.             int numberPeek = numbers[2];
  14.  
  15.             int minEl = int.MaxValue;
  16.  
  17.             int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  18.  
  19.             Stack<int> stack = new Stack<int>(input);
  20.  
  21.             //for (int i = 0; i < input.Length; i++)
  22.             //{
  23.             //    for (int j = 0; j < numberPush; j++)
  24.             //    {
  25.             //        stack.Push(input[i]);
  26.             //    }
  27.             //}
  28.  
  29.             for (int i = 0; i < numberPop; i++)
  30.             {
  31.                 stack.Pop();
  32.             }
  33.  
  34.             if (stack.Contains(numberPeek))
  35.             {
  36.                 Console.WriteLine("true");
  37.             }
  38.             else if(stack.Count == 0)
  39.             {
  40.                 Console.WriteLine("0");
  41.             }
  42.             else
  43.             {
  44.                 //    for (int i = 0; i < input.Length; i++)
  45.                 //    {
  46.                 //        if (input[i] < minEl)
  47.                 //        {
  48.                 //            minEl = input[i];
  49.                 //        }
  50.                 //    }
  51.  
  52.                 foreach (var item in stack)
  53.                 {
  54.                     if (item < minEl)
  55.                     {
  56.                       minEl = item;
  57.                     }
  58.                 }
  59.  
  60.                 Console.WriteLine(minEl);
  61.             }
  62.         }
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement