백준 알고리즘 : 숫자 야구
🔒 문제
백준 알고리즘 #2503
🔑 풀이
idea
- 세 자리 숫자를 각각 100의 자리 a, 10의 자리 b, 1의 자리 c로 지정하여 for문을 적용
- 만약 a, b, c 중 같은 숫자가 있다면 continue를 이용해 for문으로 돌아 감
- hint의 개수를 맨 위에서 입력받고, 모든 hint를 통과해야만 답이 될 가능성이 있음.
- a, b, c가 각각 존재하는지 확인하고, 위치가 같다면 strike + 1, 위치가 다르면 ball + 1
- strike와 ball의 개수가 힌트에 나온 내용과 같다면, 해당 hint 하나를 통과 -> check를 하나 증가
- 임의의 세 자리 숫자가 hint를 통과할 때마다 check를 하나씩 올리게 되면, check가 hint의 개수와 같아야 함
code
N = int(input())
hints = []
count = 0
for _ in range(N):
num, strike, ball = input().split(' ')
hint.append([num, strike, ball])
for a in range(1, 10):
for b in range(1, 10):
for c in range(1, 10):
if a == b or b == c or c == a :
continue
hint_check = 0
for hint in hints:
pred_strike = 0
pred_ball = 0
if str(a) in str(hint[0]):
if str(a) == str(hint[0])[0] :
pred_strike =+ 1
else :
pred_ball =+ 1
if str(b) in str(hint[0]):
if str(b) == str(hint[0])[1] :
pred_strike =+ 1
else :
pred_ball =+ 1
if str(c) in str(hint[0]):
if str(c) == str(hint[0])[2] :
pred_strike =+ 1
else :
pred_ball =+ 1
if pred_strike == strike and pred_ball == ball:
hint_check += 1
if len(hint) == hint_check :
count += 1
print(count)