kwest87

Untitled

Dec 29th, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp18
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string parentheses = "(((()))())";
  10.             char leftParenhesis = '(';
  11.             char rightParenthesis = ')';
  12.             int currentDepth = 0;
  13.             int maximumDepth = 0;
  14.             int controlNumber = 0;
  15.  
  16.             for (int i = 0; i < parentheses.Length; i++)
  17.             {
  18.                 if (parentheses[i] == leftParenhesis)
  19.                 {
  20.                     currentDepth++;
  21.                 }
  22.                 else if (parentheses[i] == rightParenthesis)
  23.                 {
  24.                     currentDepth--;
  25.                 }
  26.  
  27.                 if (currentDepth > maximumDepth)
  28.                 {
  29.                     maximumDepth = currentDepth;
  30.                 }
  31.  
  32.                 if(currentDepth<controlNumber)
  33.                 {
  34.                     break;
  35.                 }
  36.             }
  37.  
  38.             if (currentDepth == controlNumber)
  39.             {
  40.                 Console.WriteLine($"Корректно.Максимальная вложенность {maximumDepth}");
  41.             }
  42.             else
  43.             {
  44.                 Console.WriteLine("Не корректно.");
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment