Advertisement
nikolapetkov824

P01BasicStackOperation

Jan 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace BasicStackOperations
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] commands = Console.ReadLine()
  12.                 .Split(new string[] { " ", "/t" }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.  
  16.             int numbersToAdd = commands[0];
  17.             int numbersToPop = commands[1];
  18.             int numbersToSearchFor = commands[2];
  19.  
  20.             int[] numbers = Console.ReadLine()
  21.                 .Split(new string[] { " ","/t" }, StringSplitOptions.RemoveEmptyEntries)
  22.                 .Select(int.Parse)
  23.                 .ToArray();
  24.  
  25.             Stack<int> stack = new Stack<int>();
  26.  
  27.             for (int i = 0; i < numbersToAdd; i++)
  28.             {
  29.                 stack.Push(numbers[i]);
  30.             }
  31.  
  32.             for (int i = 0; i < numbersToPop; i++)
  33.             {
  34.                 stack.Pop();
  35.             }
  36.  
  37.             if (stack.Count == 0)
  38.             {
  39.                 Console.WriteLine(0);
  40.                 return;
  41.             }
  42.             if (stack.Contains(numbersToSearchFor))
  43.             {
  44.                 Console.WriteLine("true");
  45.             }
  46.             else
  47.             {
  48.                 Console.WriteLine(stack.Min());
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement