티스토리 뷰

📚 문제

입력

출력

예제 입력

26 5
Bulbasaur
Ivysaur
Venusaur
Charmander
Charmeleon
Charizard
Squirtle
Wartortle
Blastoise
Caterpie
Metapod
Butterfree
Weedle
Kakuna
Beedrill
Pidgey
Pidgeotto
Pidgeot
Rattata
Raticate
Spearow
Fearow
Ekans
Arbok
Pikachu
Raichu
25
Raichu
3
Pidgey
Kakuna

예제 출력

Pikachu
26
Venusaur
16
14

🧑🏻‍💻 풀이 과정

  • 파이썬의 딕셔너리를 사용하자.
  • 숫자를 입력 받았을 때, 바로 찾기 위해서 1부터 n+1만큼 반복하자.
  • 나중에 입력받을 이름과 번호를 각각 구분해서 value로 출력하기 위해 pokemon에는 이름이 key, number_pokemon에는 숫자가 key로 초기화해주자.
  • m만큼 반복하면서 입력이 숫자로 들어오면 number_pokemon의 value를 반환하고, 문자로 들어오면 pokemon의 value를 반환하자.
import sys


def pokemon_master(n, m):
    pokemon = {}
    number_pokemon = {}
    for i in range(1, n + 1):
        name = sys.stdin.readline().rstrip()
        pokemon[name] = pokemon.setdefault(name, i)
        number_pokemon[i] = pokemon.setdefault(i, name)

    for i in range(m):
        name = sys.stdin.readline().rstrip()
        if name.isnumeric():
            print(number_pokemon[int(name)])
        else:
            print(pokemon[name])


n, m = map(int, sys.stdin.readline().rstrip().split())
pokemon_master(n, m)
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함