Advertisement
silvana1303

stack sum

Sep 19th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 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.  
  13.             string[] command = Console.ReadLine().ToLower().Split().ToArray();
  14.  
  15.             Stack<int> stack = new Stack<int>(numbers);
  16.  
  17.             while (command[0] != "end")
  18.             {
  19.                 if (command[0].ToLower() == "add")
  20.                 {
  21.                     int number1 = int.Parse(command[1]);
  22.                     int number2 = int.Parse(command[2]);
  23.  
  24.                     stack.Push(number1);
  25.                     stack.Push(number2);
  26.                 }
  27.                 if (command[0] == "remove")
  28.                 {
  29.                     int count = int.Parse(command[1]);
  30.  
  31.                     if (stack.Count > count)
  32.                     {
  33.                         for (int i = 0; i < count; i++)
  34.                         {
  35.                             stack.Pop();
  36.                         }
  37.                     }
  38.                 }
  39.  
  40.                 command = Console.ReadLine().ToLower().Split().ToArray();
  41.             }
  42.  
  43.             Console.WriteLine($"Sum: {stack.Sum()}");
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement