Advertisement
Guest User

Untitled

a guest
Feb 5th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2.  
  3. namespace BalancedBrackets
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             string input = "";
  11.             int openingBracket = 0;
  12.             int closingBracket = 0;
  13.             bool isBalanced = false;
  14.  
  15.             for (int i = 1; i <= n; i++)
  16.             {
  17.                 input = Console.ReadLine();
  18.  
  19.                 if (input == "(")
  20.                 {
  21.                     openingBracket++;
  22.                 }
  23.  
  24.                 else if (input == ")")
  25.                 {
  26.                     closingBracket++;
  27.  
  28.                     if (openingBracket == closingBracket)
  29.                     {
  30.                         isBalanced = true;
  31.                         openingBracket = 0;
  32.                         closingBracket = 0;
  33.                     }
  34.  
  35.                     else
  36.                     {
  37.                         isBalanced = false;
  38.                         openingBracket = 0;
  39.                         closingBracket = 0;
  40.                     }
  41.                 }
  42.             }          
  43.  
  44.             if (openingBracket != closingBracket)
  45.             {
  46.                 isBalanced = false;
  47.             }
  48.  
  49.             if (isBalanced)
  50.             {
  51.                 Console.WriteLine("BALANCED");
  52.             }
  53.  
  54.             else
  55.             {
  56.                 Console.WriteLine("UNBALANCED");
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement