o_ignatov

problem15

Jun 2nd, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 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 _15_Balanced_Brackets
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14. string input = String.Empty;
  15. bool hasStart = false;
  16. bool balanced = false;
  17. int reminder = 0;
  18. for (int i = 1; i <= n; i++)
  19. {
  20. input = Console.ReadLine();
  21. try
  22. {
  23. char bracket = char.Parse(input);
  24. if (bracket == '(')
  25. {
  26. hasStart = true;
  27. balanced = true;
  28. }
  29. else if (hasStart && bracket == ')')
  30. {
  31. balanced = true;
  32. }
  33. else if (hasStart == false && bracket == ')')
  34. {
  35. balanced = false;
  36. break;
  37. }
  38. else if (hasStart && bracket == '(')
  39. {
  40. balanced = false;
  41. break;
  42. }
  43. }
  44. catch
  45. {
  46.  
  47. }
  48. if (balanced == false)
  49. {
  50. reminder = i;
  51. break;
  52. }
  53. reminder=i;
  54. }
  55. if (reminder < n)
  56. {
  57. for (int i = reminder; i <= n; i++)
  58. {
  59. input = Console.ReadLine();
  60. }
  61. }
  62.  
  63. if (balanced)
  64. {
  65. Console.WriteLine("BALANCED");
  66. }
  67. else
  68. {
  69. Console.WriteLine("UNBALANCED");
  70. }
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment