Advertisement
TeT91

ДЗ: Скобочное выражение

May 16th, 2024 (edited)
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLight
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string brakets = "()";
  10.             char openedBraket = '(';
  11.             char closedBraket = ')';
  12.  
  13.             int maxDeep = 0;
  14.             int currentDeep = 0;
  15.  
  16.             int half = 2;
  17.  
  18.             if (brakets.Length % half == 0)
  19.             {
  20.                 for (int i = 0; i < brakets.Length; i++)
  21.                 {
  22.                     if (brakets[i] == openedBraket)
  23.                     {
  24.                         currentDeep++;
  25.                         if (currentDeep > maxDeep)
  26.                         {
  27.                             maxDeep = currentDeep;
  28.                         }
  29.                     }
  30.                     else if (brakets[i] == closedBraket)
  31.                     {
  32.                         currentDeep--;
  33.  
  34.                         if(currentDeep < 0)
  35.                         {
  36.                             break;
  37.                         }
  38.                     }
  39.                 }
  40.  
  41.                 if (currentDeep != 0)
  42.                 {
  43.                     Console.WriteLine("Строка не корректна");
  44.                 }
  45.                 else
  46.                 {
  47.                     Console.WriteLine($"Строка корректна. Глубина {maxDeep}");
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine("Строка не корректна");
  53.             }
  54.  
  55.             Console.ReadKey();
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement