๐ฏ CodingTest/BaekJoon
[BaekJoon] 1541 ์์ด๋ฒ๋ฆฐ ๊ดํธ
S.Honey
2022. 4. 15. 10:02
โถ ๋ฌธ์ :
โถ ์ฝ๋ :
expr = input()
tempExpr = ''
tempNum = ''
# + ๊ธฐํธ๋ฅผ ํฌํจํ๋ฉด ๋ง์
์ ํด์ฃผ๋ ํจ์
def exprSum(strData):
if '+' in strData:
for i in range(len(strData)):
if strData[i] == '+':
return str(int(strData[:i]) + int(strData[i+1:]))
return strData
# ์ฐ์ ์ ์ผ๋ก ์์ ํฌํจ๋ + ๊ธฐํธ๋ฅผ ์ด์ฉํ ๋ง์
์ ์งํ ์์์ + ๊ธฐํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ชจ๋ ๋ํด์ค
for i in range(len(expr)):
if i == len(expr) - 1:
tempNum += expr[i]
tempNum = exprSum(tempNum)
tempExpr += tempNum
elif expr[i] == '-' or expr[i] == '+':
tempNum = exprSum(tempNum)
if expr[i] == '-':
tempExpr += tempNum + '-'
tempNum=''
elif expr[i]== '+':
tempNum += '+'
else:
tempNum += expr[i]
result = 0
tempNum = ''
count = 0
# ๊ดํธ๋ฅผ ์ด์ฉํด ๋ง์
์ ์งํํ ์์ ๋ํ์ฌ ๋๋จธ์ง ์์ ๊ณ์ฐ(๋บ์
๋ง ์์)
if '-' in tempExpr:
for i in range(len(tempExpr)):
if tempExpr[i] == '-':
count += 1
if count == 1:
result = int(tempNum)
tempNum=''
else:
result -= int(tempNum)
tempNum=''
elif i == len(tempExpr)-1:
tempNum += tempExpr[i]
result -= int(tempNum)
tempNum=''
else:
tempNum += tempExpr[i]
else:
result = int(tempExpr)
print(result)
โถ ๋ฌธ์ ํ์ด
1. ๊ทธ๋ฆฌ๋ํ๊ฒ ์ ๊ทผํ์๋ค.
2. ์์์ +์ฐ์ฐ์ ๋ํ ๋ถ๋ถ์ ๋ํด์ฃผ๊ณ ๋ง์ง๋ง์ ํด๋น ์๋ค์ ๋นผ์ฃผ๋ฉด ์ต์๊ฐ์ด ๋์จ๋ค.
3. โ๊ทธ๋ ๊ธฐ์ ์ฐ์ ์ ์ผ๋ก + ๊ธฐํธ ๋ถ๋ถ์ ๋ชจ๋ ๋ํด์ฃผ๋๋กํ์๊ณ , ์ดํ ๋๋จธ์ง ์์์ - ์ ๋ํ ์ฐ์ฐ์ ์งํํ๋ ๋ฐฉ์์ผ๋ก ํด๊ฒฐํ ์ ์์๋ค.