Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Stack_sum
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var input=Console.ReadLine().Split(' ').Select(int.Parse);
  12. Stack<int> stack = new Stack<int>(input);
  13. var command = Console.ReadLine().ToLower().Trim();
  14. while(command!="end")
  15. {
  16. var tokens = command.Split();
  17. var toDo = tokens[0].ToLower();
  18. if(toDo=="add")
  19. {
  20. stack.Push(int.Parse(tokens[1]));
  21. stack.Push(int.Parse(tokens[2]));
  22. }
  23. else if(toDo=="remove")
  24. {
  25. var numbersToRemove = int.Parse(tokens[1]);
  26. if(stack.Count<numbersToRemove)
  27. {
  28. continue;
  29. }
  30. for (int i = 0; i < numbersToRemove; i++)
  31. {
  32. stack.Pop();
  33. }
  34. }
  35. command = Console.ReadLine();
  36. }
  37. var sum = stack.Sum();
  38. Console.WriteLine($"Sum: {sum}");
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement