본문 바로가기
Problem Solving

[백준 BOJ - 1620 / 파이썬] 나는야 포켓몬 마스터 이다솜

by Oh Seokjin 2022. 8. 4.

1620번: 나는야 포켓몬 마스터 이다솜 (acmicpc.net)

 

1620번: 나는야 포켓몬 마스터 이다솜

첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면

www.acmicpc.net

 

🔍 문제 분석

포켓몬 도감에서 탐색을 진행함. 번호를 입력받으면 포켓몬 이름을, 포켓몬 이름을 입력 받으면 도감의 번호를 출력하는 문제

 

❗ 해결 아이디어 

번호:포켓몬 쌍의 딕셔너리와 포켓몬:도감 쌍의 딕셔너리를 따로 만들어 탐색을 진행함

 

✔️ 최종 풀이

import sys

n, m = map(int, sys.stdin.readline().split())

num_to_poke = {}

for i in range(n):
    pokemon = sys.stdin.readline().rstrip()
    num_to_poke[i+1] = pokemon

poke_to_num = {poke:num for num, poke in num_to_poke.items()}

for _ in range(m):
    answer = sys.stdin.readline().rstrip()
    if answer.isdigit():
        print(num_to_poke[int(answer)])
    else:
        print(poke_to_num[answer])

댓글