코딩테스트
[프로그래머스][C++] 숫자 문자열과 영단어
이쿠우우
2022. 3. 15. 20:18
반응형
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
글쓴이의 답
개인적인 풀이 임으로
이것보다 더 좋은 알고리즘은 많음...
이렇게도 풀이하는구나.. 공유하기 위해 올림...
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int changeNum(string temp){
int result = -1;
if(("0" == temp) || ("zero" == temp)){
result = 0;
}
else if(("1" == temp) || ("one" == temp)){
result = 1;
}
else if(("2" == temp) || ("two" == temp)){
result = 2;
}
else if(("3" == temp) || ("three" == temp)){
result = 3;
}
else if(("4" == temp) || ("four" == temp)){
result = 4;
}
else if(("5" == temp) || ("five" == temp)){
result = 5;
}
else if(("6" == temp) || ("six" == temp)){
result = 6;
}
else if(("7" == temp) || ("seven" == temp)){
result = 7;
}
else if(("8" == temp) || ("eight" == temp)){
result = 8;
}
else if(("9" == temp) || ("nine" == temp)){
result = 9;
}
return result;
}
int solution(string s) {
int answer = 0;
int location = 1;
string temp;
for(int i = s.size()-1; 0 <= i; i--){
string num;
num += s[i];
temp.insert(0, num);
int changeResult = changeNum(temp);
if (-1 != changeResult){
int result = location * changeResult;
answer += result;
location *= 10;
temp = "";
}
}
return answer;
}
꾸준히 하다보면 실력이 늘겠지.
반응형