Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

main

[C++ 공부] 8. 올바른 괄호 본문

Algorithm

[C++ 공부] 8. 올바른 괄호

1984 2022. 1. 31. 00:12

 

"it 취업을 위한 알고리즘 문제풀이 (with C, C++)  코딩테스트 대비" 강의 문제 풀이 입니다.

 

#include <iostream>
#include <string>

using namespace std;

int main(void)
{

	string str;
	int count = 0;

	getline(cin, str);

	for (string::iterator it = str.begin(); it <= str.end(); ++it)
	{
		if (it == str.end())
		{
			if (count == 0)
			{
				cout << "YES";
				break;
			}
			else
			{
				cout << "NO";
				break;
			}
		}
		if (*it == '(') count++;
		else if (*it == ')')
		{
			count--;
			if (count < 0)
			{
				cout << "NO";
				break;
			}
		}
	}

	return 0;
}

 

[참고 자료]

https://www.cplusplus.com/reference/string/string/begin/

728x90

'Algorithm' 카테고리의 다른 글

[C++ 알고리즘 공부] 10. 자릿수의 합  (0) 2022.02.01
[C++ 공부] 9. 모두의 약수  (0) 2022.01.31
[C++ 공부] 7. 영어단어 복구  (0) 2022.01.30
[백준 - 2935번] 소음  (0) 2022.01.26
[백준 - 1181번] 단어 정렬  (0) 2022.01.24
Comments