https://programmers.co.kr/learn/courses/30/lessons/81301;

 

코딩테스트 연습 - 숫자 문자열과 영단어

네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자

programmers.co.kr

 

 

#include <string>
#include <vector>
#include <map>

using namespace std;

int solution(string s) {
	int answer = 0;
	string s_answer;
	string check;
	map<string, string> dict;

	dict["zero"] = '0';
	dict["one"] = '1';
	dict["two"] = '2';
	dict["three"] = '3';
	dict["four"] = '4';
	dict["five"] = '5';
	dict["six"] = '6';
	dict["seven"] = '7';
	dict["eight"] = '8';
	dict["nine"] = '9';

	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] >= '0' && s[i] <= '9')
		{
			s_answer += s[i];
		}
		else
		{
			check += s[i];
            if (dict.find(check) != dict.end())
			{
				s_answer += dict[check];
				check.clear();
			}
		}
	}

	answer = atoi(s_answer.c_str());

	return answer;
}

int main() {
	solution("one4seveneight");
}

'알고리즘 > Coding Test' 카테고리의 다른 글

음양 더하기 C++  (0) 2022.05.19
없는 숫자 더하기 C++  (0) 2022.05.19
키패드 누르기 C++  (0) 2022.05.19
신규 아이디 추천 C++  (0) 2022.05.18
신고 결과 받기 C++  (0) 2022.05.17

+ Recent posts