백준 1620

HR·2022년 4월 3일
0

알고리즘 문제풀이

목록 보기
7/50

백준 1620 : 나는야 포켓몬 마스터

  1. map 이용해 포켓몬 이름 -> 숫자 변환
  2. N번째 포켓몬 이름은 따로 포켓몬 이름이 들어있는 배열을 이용해 변환

정답 코드

#include <iostream>
#include <map>

using namespace std;

int n, m;
string s;
map<string, int> _map;
string name[100001];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin.tie(NULL);
	
	cin>>n>>m;
	
	for(int i=0; i<n; i++) {
		cin>>s;
		_map.insert({s, i+1});
		name[i+1]=s;
	}
	
	for(int i=0; i<m; i++) {
		cin>>s;
		
		if(atoi(s.c_str())==0) { //문자열인 경우 
			auto iter=_map.find(s.c_str());
			cout<< iter->second <<"\n";
		}
		else { //숫자인 경우
			cout<<name[atoi(s.c_str())]<<"\n";
		}
	}
	
	return 0;
}
  • atoi 함수는 문자열을 받았을 경우 0을 리턴한다.
  • atoi나 find를 사용할 때에는 (string).c_str() 을 이용해서 문자열을 넘겨준다.

0개의 댓글