풀이과정
일단 string형의 빈 str을 준비한다.
그리고 substr을 사용하여 각 영단어에 대응하는 숫자를 문자열에 더해줬다.
마지막에 int로 반환을 해야 되니 stoi함수를 써서 string을 int형으로 바꿔주었다.
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int answer = 0;
string str = "";
for(int i = 0; i < s.size(); i++){
if(s.substr(i, 4) == "zero"){
str += "0";
}
else if(s.substr(i, 3) == "one"){
str += "1";
}
else if(s.substr(i, 3) == "two"){
str += "2";
}
else if(s.substr(i, 5) == "three"){
str += "3";
}
else if(s.substr(i, 4) == "four"){
str += "4";
}
else if(s.substr(i, 4) == "five"){
str += "5";
}
else if(s.substr(i, 3) == "six"){
str += "6";
}
else if(s.substr(i, 5) == "seven"){
str += "7";
}
else if(s.substr(i, 5) == "eight"){
str += "8";
}
else if(s.substr(i, 4) == "nine"){
str += "9";
}
else if("0" <= s.substr(i, 1) && s.substr(i, 1) <= "9"){
str += s[i];
}
}
answer = stoi(str);
return answer;
}
'🍞 Problem Solving > Programmers' 카테고리의 다른 글
[프로그래머스][Level1] 예산 c++ (0) | 2022.09.03 |
---|---|
[프로그래머스][Level1] 없는 숫자 더하기 c++ (0) | 2022.09.03 |
[프로그래머스][Level1] 소수 만들기 c++ (0) | 2022.09.03 |
[프로그래머스][Level1] 로또의 최고 순위와 최저 순위 c++ (0) | 2022.09.03 |
[프로그래머스][Level1] 내적 c++ (0) | 2022.09.03 |