Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Homework15
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string text = "(()(()))";
- if (text[0] == '(')
- {
- int depth = 1;
- int maxDepth = 0;
- int openBracketsCount = 1;
- for (int i = 1; i < text.Length; i++)
- {
- if (text[i] == '(')
- {
- openBracketsCount++;
- depth += 1;
- }
- else if (text[i] == ')')
- {
- openBracketsCount--;
- depth -= 1;
- if (openBracketsCount < 0)
- break;
- }
- if (maxDepth < depth)
- maxDepth = depth;
- }
- if (openBracketsCount == 0)
- Console.WriteLine($"Строка корректная и максимум глубины равняется {maxDepth}");
- else
- Console.WriteLine("Строка некорректная.");
- }
- else
- Console.WriteLine("Строка некорректная.");
- }
- }
- }
Add Comment
Please, Sign In to add comment