문제

아래 링크 참고

 

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

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

www.acmicpc.net

 

 

풀이

<이름, 번호>와 <번호, 이름> 구조의 맵을 따로 만들어서 이름이 들어오면 번호, 번호가 들어오면 이름을 출력하도록 하였다.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	cout.tie(NULL);

	int n, m;
	int cnt = 1;
	map<string, int> pokemonNum;
	map<int, string> pokemonName;

	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		string s;

		cin >> s;

		pokemonNum[s] = cnt + i;
		pokemonName[cnt + i] = s;
	}

	for (int i = 0; i < m; i++) {
		string s;

		cin >> s;

		if (s[0] >= 'A' && s[0] <= 'Z')
			cout << pokemonNum[s] << "\n";
		else
			cout << pokemonName[stoi(s)] << "\n";
	}

	return 0;
}

'백준 > 집합과 맵' 카테고리의 다른 글

1764번: 듣보잡 [C++]  (0) 2023.03.31
10816번: 숫자 카드 2 [C++]  (0) 2023.03.31
14425번: 문자열 집합 [C++]  (0) 2023.03.31
10815번: 숫자 카드 [C++]  (0) 2023.03.31

+ Recent posts