Stack Sum
using System;
using System.Collections.Generic;
using System.Linq;
namespace StackSum
{
class Program
{
static void Main(string[] args)
{
int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
var stack = new Stack<int>(input);
while (true)
{
string[] command = Console.ReadLine().ToLower().Split();
if (command[0]=="Add")
{
stack.Push(int.Parse(command[1]));
stack.Push(int.Parse(command[2]));
}
else if (command[0] == "remove")
{
var countOfRemovedNums = int.Parse(command[1]);
if (stack.Count >= countOfRemovedNums)
{
for (int i = 0; i < countOfRemovedNums; i++)
{
if (stack.Any())
{
stack.Pop();
}
}
}
}
else if(command[0]=="end")
{
break;
}
}
var sum = stack.Sum();
Console.WriteLine($"Sum: {sum}");
}
}
}
Някой били ми казал каква ми е грешката в кода?
Calculate the sum in the stack
Before that you will receive commands
Add - adds the two numbers
Remove - removes count numbers
Input |
Output |
1 2 3 4 adD 5 6 REmove 3 eNd |
Sum: 6 |
3 5 8 4 1 9 add 19 32 remove 10 add 89 22 remove 4 remove 3 end |
Sum: 16 |