[C언어] 백준 9012 : 괄호

mainsain·2022년 3월 30일
0

백준

목록 보기
59/64

생각의 흐름

이걸 스택으로 어떻게풀지? 생각했다. 뭐 하라면 할 수는 있겠지만, 그냥 '('이거는 +1, ')' 이거는 -1해서 중간에 음수가 나오면 no, 뭐 이런식으로 예외처리하면서 처리하는게 더 편해보였다. 바로 코드 짰다.

내가 푼 코드

#include <stdio.h>
#include <string.h>

int main()
{
	int n, i, j, count;
	scanf("%d", &n);
	char str[51];
	i = 0;
	while (i < n)
	{
		scanf("%s", str);
		j = 0;
		count = 0;
		while (j < strlen(str))
		{
			if (str[j] == '(')
			{
				count++;
			}
			else
			{
				count--;
			}
			if (count < 0)
			{
				printf("NO\n");
				break;
			}
			j++;
		}
		if (count == 0)
			printf("YES\n");
		else if (count > 0)
			printf("NO\n");
		i++;
	}
}

https://yahohococo.tistory.com/31 스택으로 푸는 방법. 다음에 참고해보자.

profile
새로운 자극을 주세요.

0개의 댓글