백준
[백준] 1541 잃어버린 괄호 [Python, 파이썬]
YoonJuHan
2024. 1. 10. 16:44
- 문제 : https://www.acmicpc.net/problem/1541
- 🔑 더하기 먼저
- 더하기 먼저 모두 하고 나서 빼기를 진행
- '-'를 기준으로 split을 한다.
- 55-50+40 → ['55', '50+40']
- 요소가 숫자로만 구성된 문자열이면 정수로 형변환
- 요소에 '+'가 있으면 '+'를 기준으로 split을 하고 더함 → [55, 90]
- 만들어진 리스트 요소들을 전부 빼면 정답 ✨
n = input()
s = n.split('-')
for i in range(len(s)):
if '+' in s[i]:
tmp = map(int, s[i].split('+'))
s[i] = sum(tmp)
else:
s[i] = int(s[i])
answer = s[0]
for i in range(1, len(s)):
answer -= s[i]
print(answer)