Exemple #1
0
        static void Main()
        {
            var numberOfOperations = int.Parse(Console.ReadLine());
            var myStack            = new Stack <int>();
            var stringBuilder      = new StringBuilder();

            for (int i = 0; i < numberOfOperations; i++)
            {
                var currentCommand = Console.ReadLine()
                                     .Split()
                                     .ToArray();

                var action = currentCommand[0];

                switch (action)
                {
                case "1":
                    var numToPush = int.Parse(currentCommand[1]);
                    myStack.Push(numToPush); break;

                case "2":
                    if (myStack.Count > 0)
                    {
                        myStack.Pop();
                    }
                    break;

                case "3":
                    if (myStack.Count > 0)
                    {
                        stringBuilder.AppendLine(myStack.Max().ToString());
                    }
                    break;

                case "4":
                    if (myStack.Count > 0)
                    {
                        stringBuilder.AppendLine(myStack.Min().ToString());
                    }
                    break;

                default:
                    break;
                }
            }

            stringBuilder.Append(string.Join(", ", myStack));
            Console.WriteLine(string.Join(Environment.NewLine, stringBuilder));
        }
        static void Main()
        {
            ulong numbEntries = ulong.Parse(Console.ReadLine());

            var   numStack = new Stack <ulong>();
            ulong maxVal   = 0;

            for (ulong i = 0; i < numbEntries; i++)
            {
                ulong[] ulLine = Console.ReadLine()
                                 .Trim()
                                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                 .Select(ulong.Parse)
                                 .ToArray();

                switch (ulLine[0])
                {
                case 1:
                    //ulong bNumber = ulLine[1];
                    numStack.Push(ulLine[1]);
                    if (ulLine[1] > maxVal)
                    {
                        maxVal = ulLine[1];
                    }
                    break;

                case 2:
                    var elem = numStack.Pop();
                    if (numStack.Count > 0 && elem == maxVal)
                    {
                        maxVal = numStack.Max();
                    }
                    else if (numStack.Count == 0 && elem == maxVal)
                    {
                        maxVal = 0;
                    }
                    break;

                case 3:
                    Console.WriteLine(maxVal);
                    break;
                }
            }
        }