Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Stack_sum
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input=Console.ReadLine().Split(' ').Select(int.Parse);
- Stack<int> stack = new Stack<int>(input);
- var command = Console.ReadLine().ToLower().Trim();
- while(command!="end")
- {
- var tokens = command.Split();
- var toDo = tokens[0].ToLower();
- if(toDo=="add")
- {
- stack.Push(int.Parse(tokens[1]));
- stack.Push(int.Parse(tokens[2]));
- }
- else if(toDo=="remove")
- {
- var numbersToRemove = int.Parse(tokens[1]);
- if(stack.Count<numbersToRemove)
- {
- continue;
- }
- for (int i = 0; i < numbersToRemove; i++)
- {
- stack.Pop();
- }
- }
- command = Console.ReadLine();
- }
- var sum = stack.Sum();
- Console.WriteLine($"Sum: {sum}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement