Coding Test - cpp/Greedy
[백준 1541: 잃어버린 괄호] - C++
에드윈H
2021. 3. 5. 16:52
#include<iostream>
#include<string>
using namespace std;
int main() {
int num = 0;
string n;
string temp;
bool change = false;
cin >> n;
for (int i = 0; i <= n.size(); ++i) {
if (n[i] == '+' || n[i] == '-' || n[i] == '\0') {
if (change) {
num -= stoi(temp);
}
else {
num += stoi(temp);
}
if (n[i] == '-') {
change = true;
}
temp = "";
continue;
}
temp += n[i];
}
cout << num << endl;
return 0;
}