4949번: 균형잡힌 세상 (acmicpc.net)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _230126_1
{
    class Program
    {
        

        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();

            while (true)
            {
                string strArr = sr.ReadLine();
                Stack<char> stack = new Stack<char>();
                bool isYes = true;

                for (int i = 0; i < strArr.Length; i++)
                {
                    
                    if (strArr[i] == '(' || strArr[i] == '[')
                    {
                        stack.Push(strArr[i]);
                    }

                    if (strArr[i] == ')')
                    {
                        if (stack.Count <= 0)
                        {
                            isYes = false;
                            break;
                        }

                        char rightBracket = stack.Pop();

                        if (rightBracket != '(')
                        {
                            isYes = false;
                            break;
                        }
                    }

                    if (strArr[i] == ']')
                    {
                        if (stack.Count <= 0)
                        {
                            isYes = false;
                            break;
                        }

                        char rightBracket = stack.Pop();

                        if (rightBracket != '[')
                        {
                            isYes = false;
                            break;
                        }
                    }
                }

                if (strArr[0] == '.') break;

                if (isYes && stack.Count == 0) sb.AppendLine("yes");
                else sb.AppendLine("no");
            }            

            sw.Write(sb);

            sr.Close();
            sw.Close();
        }
    }
}

전체적으로 조건문을 간소화 할 수 있겠지만 정상적으로 작동하여 이정도로 마무리하였다.

 

'Algorithm > BOJ' 카테고리의 다른 글

C#) [BOJ] 10162 전자레인지  (0) 2023.01.31
C#) [BOJ] 10815 숫자 카드★  (0) 2023.01.28
C#) [BOJ] 10828 스택  (0) 2023.01.26
C#) [BOJ] 10845 큐  (0) 2023.01.26
C#) [BOJ] 2591 숫자 카드 2  (0) 2023.01.25

+ Recent posts