ranee

скобки

Sep 9th, 2022 (edited)
1,163
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 1 0
  1. using System;
  2. namespace Lectures
  3. {
  4.     class Program
  5.     {
  6.         static void Main()
  7.         {
  8.             string staples = "((())()())()";
  9.             int currentDepth = 0;
  10.             int maximumInvestment = 0;
  11.             bool isWork = false;
  12.  
  13.             foreach (var symbol in staples)
  14.             {
  15.                 switch (symbol)
  16.                 {
  17.                     case '(':
  18.                         currentDepth++;
  19.  
  20.                         if (currentDepth > maximumInvestment)
  21.                         {
  22.                             maximumInvestment = currentDepth;
  23.                         }
  24.                         break;
  25.                     case ')':
  26.  
  27.                         if (currentDepth > 0)
  28.                         {
  29.                             currentDepth--;
  30.                         }
  31.  
  32.                         else
  33.                         {
  34.                             isWork = true;
  35.                         }
  36.                         break;
  37.                 }
  38.             }
  39.  
  40.             if (!isWork)
  41.             {
  42.                 Console.WriteLine($"Строка корректна, максимальная глубина равна {maximumInvestment}.");
  43.             }
  44.             else
  45.             {
  46.                 Console.WriteLine($"Некорректная строка, максимальная глубина равна {maximumInvestment}.");
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment