풀이 소요시간 : 정석 풀이 참고
문자열 유형의 문제는 우선 몇 가지 정석 풀이를 참고하며 학습하는 것이 도움이 될 것 같다고 판단하여 풀이를 참고하였다. 문자열 + 그리디 문제였는데, - 부호가 나타나면 그 뒤의 숫자는 모두 -연산을 해주면 되는 문제였다.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
bool status = true;
string str;
cin >> str;
int total = 0;
string temp = "";
for (int i = 0; i < str.length(); i++) {
if (str[i] != '+' && str[i] != '-') {
temp += str[i];
}
if (str[i] == '+' || str[i] == '-' || i == str.length() - 1) {
if (status == true) {
total += stoi(temp);
temp = "";
}
else {
total -= stoi(temp);
temp = "";
}
}
if (str[i] == '-') {
status = false;
}
}
cout << total;
}