백준 #4900 7 더하기
문제 링크
문제 설명
$0~9$사이의 숫자를 LED전광판으로 표시할 때 특정 규칙이 있고 규칙에 따라 표현한 식을 계산하는 문제
코드
import sys
input = sys.stdin.readline
light = [[0, 1, 2, 3, 4, 5], [1, 3], [0, 3, 6, 4, 2], [0, 3, 6, 1, 2], [5, 6, 3, 1], [0, 5, 6, 1, 2], [0, 5, 6, 1, 2, 4], [0, 3, 1], [0 ,1, 2, 3, 4, 5, 6], [0, 3, 5, 6, 1]]
ntoc = {}
cton = {}
for i in range(10):
x = 0
code = ""
for j in light[i]:
x += 2 ** j
if x < 100:
code = "0" + str(x)
else:
code = str(x)
ntoc[i] = code
cton[code] = i
while True:
sentence = input().rstrip()
if sentence == "BYE":
break
sentence = sentence[:-1]
left, right = sentence.split("+")
L, R = len(left), len(right)
left_num = ""
right_num = ""
for i in range(0, L, 3):
code = ""
for j in range(i, i + 3):
code += left[j]
left_num += str(cton[code])
for i in range(0, R, 3):
code = ""
for j in range(i, i + 3):
code += right[j]
right_num += str(cton[code])
res = int(left_num) + int(right_num)
res_code = ""
for x in str(res):
res_code += ntoc[int(x)]
print(left+"+"+right+"="+res_code)
풀이
$0~9$사이의 숫자를 규칙에 따라 코드로 변형해 준 후에 숫자를 코드로 변경해주는 dict
인 ntoc
와 코드를 숫자로 변경해주는 cton
에 각각 넣어주었다. 주어진 식을 코드 단위로 쪼개서 숫자로 변형해주어 식을 계산한 뒤 다시 코드로 변환했다.
벌써 7월이다..시간 진짜 빠른듯..
댓글남기기