#include <string> #include <vector> #include <iostream> #include <cmath> #include <map> #include <algorithm> #include <sstream> using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); string str; cin >> str; stringstream ss(str); vector<string> v; int n; char c; ss >> n; int sum = n; int save=0; while(!ss.eof()) { ss >> c; ss >> n; if(c == '+'){ if(save == 0) sum += n; else save += n; }else if(c == '-'){ if(save == 0) save += n; else { sum -= save; save = n; } } } if(save != 0) sum -= save; cout << sum; return 0; }
- 원리
: 최대한 많은 수를 빼는 것이 핵심- 로직
1) -가 나오면 다음 -가 나올 때 까지 수를 save에 쌓는다
2) 다음 -가 나오면 쌓인 save를 빼고 다시 save를 지금 읽은 수로 치환