Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 1541 잃어버린 괄호 [Python, 파이썬] 본문
- 문제 : 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)
'백준' 카테고리의 다른 글
[백준] 1765 닭싸움 팀 정하기 [Python, 파이썬] (0) | 2024.01.17 |
---|---|
[백준] 1043 거짓말 [Python, 파이썬] (0) | 2024.01.16 |
[백준] 2307 도로검문 [Python, 파이썬] (1) | 2024.01.09 |
[백준] 1202 보석 도둑 [Python, 파이썬] (0) | 2024.01.05 |
[백준] 1874 스택 수열 [Python, 파이썬] (0) | 2024.01.05 |
Comments