Greedy
์ธ ๋ฒ์งธ ๋ฌธ์ ~!
์ด๊ฒ๋ ๊ฐ๋จํ ๋ฌธ์ ์๋ค.
import re
_input = input()
numbers = re.findall('\d+', _input)
strings = re.findall('\D+', _input)
_sum, bump = int(numbers[0]), 0
for i in range(len(strings)):
num = int(numbers[i+1])
strr = strings[i]
if strr == '+':
if bump:
bump += num
else:
_sum += num
elif strr == '-':
if bump:
_sum -= bump
bump = num
if bump:
_sum -= bump
print(_sum)
re
๋ชจ๋์ผ๋ก ์ ๊ท์์ ์ด์ฉํด์ ํ์๋ค.
a = input().split('-')
num = []
for i in a:
cnt = 0
s = i.split('+')
for j in s:
cnt += int(j)
num.append(cnt)
n = num[0]
for i in range(1, len(num)):
n -= num[i]
print(n
์ด ์ฝ๋๊ฐ ๋ ์ง๊ด์ ์ธ ๊ฒ ๊ฐ์์ ์ฒจ๋ถํ๋ค.
wikidocs ์ ๊ท์
https://wikidocs.net/4308
[๋ฐฑ์ค] 1541๋ฒ(python ํ์ด์ฌ)
https://pacific-ocean.tistory.com/228