Coding - Algo/python
[백준] 2503번:숫자 야구 (python 파이썬)
jainn
2021. 1. 18. 23:17
728x90
2503번: 숫자 야구
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트
www.acmicpc.net
풀이
1. num 리스트에 순열(permutations)을 사용하여 가능한 숫자 조합을 담는다.
permutations 예시
('A', 'B')와 ('B', 'A')는 다른 것이다.
import itertools
arr = ['A', 'B', 'C']
nPr = itertools.permutations(arr, 2)
print(list(nPr))
결과 : [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
2. num 을 하나하나 입력값(test) 와 대조해본다.
자리까지 같다면 스트라이크 값 ++, 자리는 다르지만 숫자가 존재하면 볼 값 ++
만약 num[i]와 입력값(test) 의 스트라이크 값과 볼 값이 일치하지 않는다면 num.remove(num[i]) 시켜준다.
3. num에는 영수가 생각하고 있을 가능성이 있는 숫자만 들어있기 때문에 길이를 출력하면 된다.
소스코드
import sys
from itertools import permutations
n = int(input())
num = list(permutations([1,2,3,4,5,6,7,8,9], 3)) //미리 넣어놓는다.
for _ in range(n):
test, s, b = map(int, input().split())
test = list(str(test))
remove_cnt = 0
for i in range(len(num)):
s_cnt = b_cnt = 0
i -= remove_cnt
for j in range(3):
test[j] = int(test[j])
if test[j] in num[i]:
if j == num[i].index(test[j]):
s_cnt += 1
else:
b_cnt += 1
if s_cnt != s or b_cnt != b:
num.remove(num[i])
remove_cnt += 1
print(len(num))
출처 : li-fo.tistory.com/m/41
반응형