Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BalancedBrackets
  8. {
  9. class Program
  10. {
  11. static void Main()
  12. {
  13. string[] arr = new string[2];
  14. byte n = byte.Parse(Console.ReadLine());
  15. string result = "BALANCED";
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. string str = Console.ReadLine();
  20.  
  21. switch (str)
  22. {
  23. case "(":
  24. // Ако вече има вкарана скоба в масива, връща "UNALANCED",
  25. // защото е отваряща и трябва да е първата
  26. if (arr[1] == ")" || arr[0] == "(")
  27. {
  28. result = "UNBALANCED";
  29. }
  30. // Ако е първата скоба (няма скоби в масива), я вкарва в масива
  31. else
  32. {
  33. arr[0] = str;
  34. }
  35. break;
  36. case ")":
  37. // Ако има отваряща скоба преди нея в масива, изчиства масива за да може да работи първия case
  38. if (arr[0] == "(")
  39. {
  40. Array.Clear(arr, 0, 1);
  41. }
  42. // Ако няма отваряща скоба преди нея в масива, връща "UNBALANCED"
  43. else
  44. {
  45. result = "UNBALANCED";
  46. }
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. Console.WriteLine(result);
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement