algorithm
[프로그래머스] 숫자 문자열과 영단어 (C++)
지제로
2022. 9. 27. 11:26
풀이과정
1. 숫자면 collect에 넣는다.
2. 문자면 tmp에 하나씩 넣어서 해당 문자열의 번호를 찾는다.
3. collect에 넣은 숫자들을 answer에 더해준다.
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int solution(string s) {
int i,j;
int answer = 0;
string tmp="";
vector<string> word{"zero","one","two","three","four","five","six","seven","eight","nine"};
vector <int> collect;
for(i=0;i<s.size();i++){
if(s[i]>=48 && s[i]<=57)
collect.push_back(s[i]-48);
else{
tmp+=s[i];
for(j=0;j<word.size();j++){
if(tmp==word[j]){
tmp="";
collect.push_back(j);
break;
}
}
}
}
for(i=0;i<collect.size(); i++)
answer+=pow(10, collect.size()-i-1)*collect[i];
return answer;
}