W1thr

Домашка №16

Jun 4th, 2022 (edited)
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Homework15
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const string text = "(()(()))";
  10.  
  11.             if (text[0] == '(')
  12.             {
  13.                 int depth = 1;
  14.  
  15.                 int maxDepth = 0;
  16.                 int openBracketsCount = 1;
  17.  
  18.                 for (int i = 1; i < text.Length; i++)
  19.                 {
  20.                     if (text[i] == '(')
  21.                     {
  22.                         openBracketsCount++;
  23.                         depth += 1;
  24.                     }
  25.                     else if (text[i] == ')')
  26.                     {
  27.                         openBracketsCount--;
  28.                         depth -= 1;
  29.  
  30.                         if (openBracketsCount < 0)
  31.                             break;
  32.                     }
  33.  
  34.                     if (maxDepth < depth)
  35.                         maxDepth = depth;
  36.                 }
  37.  
  38.                 if (openBracketsCount == 0)
  39.                     Console.WriteLine($"Строка корректная и максимум глубины равняется {maxDepth}");
  40.                 else
  41.                     Console.WriteLine("Строка некорректная.");
  42.             }
  43.             else
  44.                 Console.WriteLine("Строка некорректная.");
  45.         }
  46.     }
  47. }
  48.  
  49.  
Add Comment
Please, Sign In to add comment