Advertisement
Rodunskiy

Untitled

May 2nd, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. char symbol1 = '(';
  2. char symbol2 = ')';
  3. string symbolLine = "())(()";
  4. int maximumDepth = 0;
  5. int symbolsCount = 0;
  6.  
  7. for (int i = 0; i < symbolLine.Length; i++)
  8. {
  9.     if (symbolLine[i] == symbol1)
  10.     {
  11.         symbolsCount++;
  12.  
  13.         if (symbolsCount > maximumDepth)
  14.         {
  15.             maximumDepth = symbolsCount;
  16.         }
  17.     }
  18.     else if (symbolLine[i] == symbol2)
  19.     {
  20.         symbolsCount--;
  21.  
  22.         if (symbolsCount < 0)
  23.         {
  24.             break;
  25.         }
  26.     }
  27. }
  28.  
  29. if (symbolsCount != 0)
  30. {
  31.     Console.WriteLine("Не корректная запись.");
  32. }
  33. else
  34. {
  35.     Console.WriteLine($"Строка корректная. Максимальная глубина - {maximumDepth}.");
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement