문제 링크: https://www.acmicpc.net/problem/1620
백준 집합과 맵 3단계 - 1620번 나는야 포켓몬 마스터 이다솜을 풀어보았다.
풀이: 입력을 받아, 숫자가 들어오면 이름을 이름이 들어오면 숫자를 내보내면 된다.
C++의 경우, https://tree-water.tistory.com/116 이 코드를 참고했다.
파이썬의 경우, 딕셔너리를 써서 시간을 줄였다.
C++
#include <iostream>
#include <string.h>
#include <map>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
map<int, string> mpInt;
map<string, int> mpStr;
int n, m, idx = 0;
cin >> n >> m;
while (n--)
{
idx++;
string str;
cin >> str;
mpInt.insert(make_pair(idx, str));
mpStr.insert(make_pair(str, idx));
}
while (m--)
{
char arr[21];
cin >> arr;
if (isdigit(arr[0]))
{
int intArr = atoi(arr);
cout << mpInt.find(intArr)->second << "\n";
}
else
{
cout << mpStr.find(arr)->second << "\n";
}
}
}
Python
import sys
n,m=map(int,sys.stdin.readline().rstrip().split())
pokemon={}
for i in range(n):
a=sys.stdin.readline().rstrip()
pokemon[a]=i+1 # 숫자와 이름 모두 저장
pokemon[i+1]=a
for i in range(m):
inp=sys.stdin.readline().rstrip()
if inp.isdigit(): print(pokemon[int(inp)]) # 숫자일경우
else: print(pokemon[inp])
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
듣보잡 풀이 (0) | 2023.01.06 |
---|---|
숫자 카드 2 풀이 (0) | 2023.01.06 |
문자열 집합 풀이 (0) | 2023.01.06 |
숫자 카드 풀이 (0) | 2023.01.06 |
영화감독 숌 풀이 (0) | 2023.01.06 |