프로그래머스 120864 숨어있는 숫자의 덧셈 (2)

Jmnote (토론 | 기여)님의 2023년 11월 29일 (수) 01:02 판
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

프로그래머스 120864 숨어있는 숫자의 덧셈 (2)

2 C++[ | ]

#include <string>
#include <vector>
#include <regex>

using namespace std;

int solution(string my_string) {
    regex rx("[0-9]+");
    sregex_token_iterator iter(my_string.begin(), my_string.end(), rx, 0), end;
    vector<string> strs{iter, end};
    int answer = 0;
    for (string& s: strs) {
        answer += stoi(s);
    }
    return answer;
}
#include <string>
#include <vector>
#include <sstream>
using namespace std;

int solution(string my_string) {
    for(auto& v: my_string) {
        if(!isdigit(v)) v = ' ';
    }
    stringstream ss;
    ss.str(my_string);
    int answer=0;
    int x;
    while(true) {
        ss >> x;
        if(!ss) break;
        answer += x;
    }
    return answer;
}