알고리즘 연습

C++ 1541 잃어버린 괄호

728x90

#include <iostream>
#include<string>

using namespace std;


int main() {
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);

	string str;
	string tmp = "";
	int res = 0;
	bool minus = false;//빼기 확인용

	cin >> str;
	for (int i = 0; i <= str.size(); i++) {
		if (str[i] == '+' || str[i] == '-' || str[i] == '\0') { //문자열이 부호라면
			
			if (minus) //minus가 true일때
				res -= stoi(tmp);
			else
				res += stoi(tmp);

			tmp = ""; //숫자는 초기화 해준다.

			if (str[i] == '-') //-가 나오면 다음것들은 다 빼버린다
				minus = true;
			continue;
		}
		tmp += str[i]; //부호가 나오기 전까진 숫자이다(1+2+3=123)
	}
	cout << res;
}

어렵다.

728x90

'알고리즘 연습' 카테고리의 다른 글

C++11098 첼시를 도와줘  (0) 2021.10.11
C++ 1977 완전제곱수  (0) 2021.10.11
C++ 1931 회의실 배정  (0) 2021.10.07
C++ 11047 동전0 (그리디알고리즘)  (0) 2021.10.06
C++ 11650 좌표 정렬하기  (1) 2021.10.01