CodingTest/백준/Silver/1541. 잃어버린 괄호 at main · kwon1232/CodingTest
This is an auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - kwon1232/CodingTest
github.com
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
std::cout.tie(NULL);
string str = "", temp = "";
int result = 0;
cin >> str;
bool minus = false;
for (int i = 0; i <= str.size(); i++)
{
// 부호를 만났을 때
if (str[i] == '+' || str[i] == '-' || str[i] == '\0')
{
// 이전 값이 - 였다면 이전 값을 모두 빼준다.
if (minus) {
result -= stoi(temp);
}
// 이전 값이 -가 아니었다면 모두 더해준다
else {
result += stoi(temp);
}
// 현재 부호가 - 라면 이전 값이 -가 된다는 걸 알려준다.
temp = "";
if (str[i] == '-')
{
minus = true;
}
}
// 숫자라면 모두 더해준다.
else
{
temp += str[i];
}
}
cout << result;
return 0;
}
'콘솔창 & 윈도우창 > 코딩 테스트' 카테고리의 다른 글
프로그래머스 LV.2 괄호 회전하기 (1) | 2025.07.12 |
---|---|
백준 골드 4 최단경로 1753 (0) | 2025.06.14 |
프로그래머스 LV.2 숫자 변환하기 (0) | 2025.05.14 |
프로그래머스 LV.2 뒤에 있는 큰 수 찾기 (0) | 2025.04.29 |
[백준 골드 5] 1456 거의 소수 (0) | 2025.04.26 |