백준 1541번

DARTZ·2023년 4월 5일
0

알고리즘

목록 보기
107/135
import sys

inputValue = sys.stdin.readline()

def solution(inputValue):

    minusList = inputValue.split("-")
    caculateList = []
    answer = 0

    for m in minusList:
        p = m.split("+")
        cnt = 0
        for i in p:
            cnt += int(i)
        caculateList.append(cnt)

    answer += caculateList[0]

    for c in range(1, len(caculateList)):
        answer -= int(caculateList[c])

    return answer


print(solution(inputValue))

-가 있을 경우 다 빼버리면 원하는 값을 얻을 수 있다. 원래는 각 문자열을 돌면서 구분하려고 했는데 로직이 복잡해서 정답코드를 찾아봤더니 간단하게 split을 사용해서 분리를 했다.

profile
사람들이 비용을 지불하고 사용할 만큼 가치를 주는 서비스를 만들고 싶습니다.

0개의 댓글