https://github.com/cjk09083/Code
https://www.acmicpc.net/problem/1107
1107번: 리모컨
첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼
www.acmicpc.net
해답)
import sys
input = sys.stdin.readline
def isBtnOnly():
btnOnly = True
for i in str(t):
if i in err:
btnOnly = False
break
return btnOnly
def getNum():
if isBtnOnly():
return min(abs(t-100),len(str(t)))
start = 0
end = 1_000_000
closestNum = abs(t - 100)
for i in range(start, end):
tmp = str(i)
l = len(tmp)
isBreak = False
for j in range(l):
if tmp[j] in err:
isBreak = True
break
if not isBreak:
c = abs(t - i) + l
closestNum = min(c, closestNum)
return closestNum
if __name__ == "__main__":
t = int(input())
n = int(input())
if t == 100:
cnt = 0
elif n == 0:
cnt = min(abs(t-100),len(str(t)))
elif n == 10:
cnt = abs(t-100)
else:
err = list(map(str, input().split()))
cnt = getNum()
print(cnt)
- closestNum를 0부터 999999까지 올려가면서 가장 가까운 숫자를 찾는다.
- 708ms
'미래의 나를 위한 메모 > 코딩테스트' 카테고리의 다른 글
[230117] 백준 6064번 - 카잉달력 (Python) (0) | 2023.01.17 |
---|---|
[230116] 백준 14500번 - 테트로미노 (Python) (2) | 2023.01.16 |
[230112] 백준 1476번 - 날짜 계산 (Python) (0) | 2023.01.12 |
[230111] 백준 3085번 - 사탕게임 (Python) (0) | 2023.01.11 |
[230111] 백준 2309번 - 일곱난쟁이(Python) (0) | 2023.01.11 |