Tag: parsing
All the articles with the tag "parsing".
BOJ1541SILVER 2
예를 들면 55-50+40+90-100+10-11 이라는 수식이 입력됐을 때 55-(50+40+90)-(100+10)-11 = 55-50-40-90-100-10-11 로 만들 수 있다.
st = input().strip()+ '+'
total = 0
minus = False
num = ''
for c in st:
if c.isdigit():
num += c
else:
if minus:
total -= int(num)
else:
total += int(num)
if c == '-':
minus = True
num = ''
print(total)
입력은 숫자와 부호가 섞인 문자열로 주어지기 때문에 그 문자열을 확인하면서 문자가 숫자인지 부호인지에 때라 다르게 처리를 해주어야 한다.
잃어버린 괄호
백준 1541번 '잃어버린 괄호' (실버 2) 문제 풀이. math, string, greedy algorithm 로 접근했다.