Advertisement
KeepCoding

DataTypesMoreExProblem15

Feb 10th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace RandomExercise
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.  
  11.             byte n = byte.Parse(Console.ReadLine());
  12.             byte open = 0;      // OPENING BRACKETS
  13.             byte close = 0;     // CLOSING BRACKETS
  14.  
  15.             bool balanced = true;
  16.  
  17.             for (byte i = 1; i <= n; i++)
  18.             {
  19.                 string input = Console.ReadLine();
  20.  
  21.                 switch (input)
  22.                 {
  23.                     case "(":
  24.                         open++;
  25.                         break;
  26.                     case ")":
  27.                         if (open == 0)
  28.                         {
  29.                             balanced = false; // no opening bracket before closing bracket
  30.                         }
  31.                         else
  32.                         {
  33.                             close++;
  34.                         }
  35.                         break;
  36.                 }
  37.  
  38.                 if (open == 1 && close == 1) // RESET
  39.                 {
  40.                     open--;
  41.                     close--;
  42.                 }
  43.  
  44.             }
  45.  
  46.             if (open == 0 && close == 0 && balanced)
  47.             {
  48.                 Console.WriteLine("BALANCED");
  49.             }
  50.             else
  51.             {
  52.                 Console.WriteLine("UNBALANCED");
  53.             }
  54.  
  55.         }
  56.  
  57.  
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement