Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _2.StackSum
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
- Stack<int> numbers = new Stack<int>();
- foreach (var num in input)
- {
- numbers.Push(num);
- }
- while (true)
- {
- string[] arguments = Console.ReadLine().Split();
- string command = arguments[0];
- if (command == "end")
- break;
- if (command == "add")
- {
- int firstNumber = int.Parse(arguments[1]);
- int secondNumber = int.Parse(arguments[2]);
- numbers.Push(firstNumber);
- numbers.Push(secondNumber);
- }
- else if (command == "remove")
- {
- int count = int.Parse(arguments[1]);
- if (count <= numbers.Count)
- {
- for (int i = 0; i < count; i++)
- {
- numbers.Pop();
- }
- }
- }
- }
- Console.WriteLine($"Sum: {numbers.Sum()}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement