๋ฐ์ํ
    
    
    
  ๐ป ๋ฐฑ์ค 1541๋ฒ [์์ด๋ฒ๋ฆฐ ๊ดํธ]
1541๋ฒ: ์์ด๋ฒ๋ฆฐ ๊ดํธ
์ฒซ์งธ ์ค์ ์์ด ์ฃผ์ด์ง๋ค. ์์ โ0โ~โ9โ, โ+โ, ๊ทธ๋ฆฌ๊ณ โ-โ๋ง์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ , ๊ฐ์ฅ ์ฒ์๊ณผ ๋ง์ง๋ง ๋ฌธ์๋ ์ซ์์ด๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ์ํด์ ๋ ๊ฐ ์ด์์ ์ฐ์ฐ์๊ฐ ๋ํ๋์ง ์๊ณ , 5์๋ฆฌ๋ณด๋ค
www.acmicpc.net
ํ์ด)
# ์ต์ข
์ ์ถ
import re
eq = input()
num_lst = re.split('[+|-]',eq)
stack = []
stack.append(int(num_lst[0]))
i = 1
for ch in eq : 
    if ch.isnumeric() == False :
        if ch == '+' :
            stack.append(int(stack.pop()) + int(num_lst[i]))
        else :
            stack.append(int(num_lst[i]))
        i+=1
print(stack)
res = stack[0]
for i in range(1,len(stack)) :
    res -= stack[i]        
print(res)
# ==> ๋ฉ๋ชจ๋ฆฌ : 330K4B, ์๊ฐ : 140ms, ์ฝ๋๊ธธ์ด : 388B
728x90
    
    
  ๋ฐ์ํ
    
    
    
  '๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค > greedy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [๋ฐฑ์ค 1931] [greedy] ํ์์ค ๋ฐฐ์  โ (1) | 2021.04.18 | 
|---|---|
| [๋ฐฑ์ค 11047] [greedy] ๋์  0 (0) | 2021.04.17 | 
| [๋ฐฑ์ค 11399] [greedy] ATM (0) | 2021.04.17 | 
| [๋ฐฑ์ค 2839] [greedy] ์คํ๋ฐฐ๋ฌ (0) | 2021.04.17 |