๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด, 2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ) C++

by UKHYUN22 2021. 12. 29.
728x90

 

์ž…๋ ฅ ๋ฐ›์€ ์˜์–ด์™€ ์ˆซ์ž๊ฐ€ ์กฐํ•ฉ๋œ ๋ฌธ์ž์—ด์—์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ต๋ฅผ ํ†ตํ•ด case๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์˜์–ด ๋ฌธ์ž์—ด์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ ์ •ํ•ด์ง„ ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ ์ƒ๊ฐํ•ด์„œ one ๋ถ€ํ„ฐ nine๊นŒ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ–ˆ๋‹ค.

 

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int solution(string s) {
    int answer = 0;
    string arr = "";
    
    for(int i = 0 ; i < s.size() ; i++) {
        if(s[i] >= '0' && s[i] <= '9') {
            arr.push_back(s[i]);
            continue;
        }
        
        if(s[i] == 'z' && s.substr(i,4) == "zero") { 
            arr.push_back('0'); 
            i = i + 3;
        }
        else if(s[i] == 'o' && s.substr(i,3) == "one") { 
            arr.push_back('1');
            i = i + 2;
        }
        else if(s[i] == 't' && s.substr(i,3) == "two") { 
            arr.push_back('2'); 
            i = i + 2;
        }
        else if(s[i] == 't' && s.substr(i,5) == "three") { 
            arr.push_back('3'); 
            i = i + 4;
        }
        else if(s[i] == 'f' && s.substr(i,4) == "four") { 
            arr.push_back('4'); 
            i = i + 3;
        }
        else if(s[i] == 'f'  && s.substr(i,4) == "five") { 
            arr.push_back('5'); 
            i = i + 3;
        }
        else if(s[i] == 's' && s.substr(i,3) == "six") { 
            arr.push_back('6'); 
            i = i + 2;
        }
        else if(s[i] == 's' && s.substr(i,5) == "seven") { 
            arr.push_back('7');
            i = i + 4;
        }
        else if(s[i] == 'e' && s.substr(i,5) == "eight") { 
            arr.push_back('8'); 
            i = i + 4;
        }
        else if(s[i] == 'n' && s.substr(i,4) == "nine") { 
            arr.push_back('9'); 
            i = i + 3;
        }
        else continue;
    }
    answer = stoi(arr);
    cout << arr << endl;
    return answer;
    
}