728x90
다 풀었는데 실패가 떴고, 몇몇 반례들이 통과가 안됐으나 이유를 못찼았다. 답을 보니 처음부터 탐색을 해도 풀렸다. 시간 초과 때문에 가운데 부터 탐색을 했는데 안그랬으면 통과했을 것 같다.
import math
def find(n,possible):
for i in range(0,len(n)):
if n[i] not in possible:
return False
return True
a = int(input()) # 찾을 숫자
b = int(input())
if b == 0:
print(len(list(str(a))))
else:
k = list(map(int,input().split(" ")))
# 최대한 근접한 수를 누르고 나머지는 차이를 + 해주기
answer1 = abs(100-a)
answer2 = 0
possible = [ ] # 클릭 가능한 버튼 리스트
for i in range(0,10):
if i not in k:
possible.append(str(i))
index = 0
if len(possible)==0:
print(answer1)
else:
while True:
first = a + index
second = a - index
first = list(str(first))
second = list(str(second))
if find(first,possible)==True or find(second,possible)==True:
break
index += 1
answer2+=1
answer2 += len(list(str(a)))
print(min(answer1,answer2))
728x90
'🟢 알고리즘 문제 풀이 > Baekjoon' 카테고리의 다른 글
| [파이썬] 백준 11286 절댓값 힙 (0) | 2023.03.21 |
|---|---|
| [파이썬] 백준 1475 방 번호 (0) | 2023.03.20 |
| [파이썬] 백준 1759 암호 만들기 (0) | 2023.03.19 |
| [파이썬] 백준 7576 토마토 (0) | 2023.03.13 |
| [파이썬] 백준 23561 Young한 에너지는 부족하다 (0) | 2023.03.13 |