#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
string str;
string temp;
cin >> str;
vector<int> num;
for(int i = 0; i < str.size(); i++)
{
if(isdigit(str[i]))
{
temp += str[i];
}
else
{
if(!temp.empty())
{
num.push_back(stoi(temp));
temp.clear();
}
}
}
if(!temp.empty())
num.push_back(stoi(temp));
int sum = 0;
for(int i = 0; i < num.size(); i++)
sum += num[i];
cout << sum << "\n";
return 0;
}
1. str 입력을 받고 문자 하나씩 확인하면서 isdigit()로 숫자라면 temp에 담는다.
2. 숫자 이후 다시 알파벳을 만나면 temp에 담아두었던 문자열을 숫자로 변환하는 stoi()로 변환하여 vector num에 할당한다.
3. 문자열 마지막에 숫자가 있다면 temp가 빈 문자열이 아닐 것이므로 for문이 끝나면 한 번 더 체크한다.
4. vector num의 모든 원소를 더하여 출력한다.
'CodingTest Exam > 코딩테스트 실전 모의고사(with C++) : 대기업 대비' 카테고리의 다른 글
2-3. 멀티태스킹(효율성) ★★★★☆ (0) | 2023.09.15 |
---|---|
2-2. 모든 아나그램 (해쉬, 슬라이딩 윈도우) ★★★☆☆ (0) | 2023.09.15 |
1-5. 효율적인 공부 ★★★☆☆ (0) | 2023.09.14 |
1-4. 바둑대회(DFS) ★★★★☆ (0) | 2023.09.14 |
1-3. 영화 관람 ★★★☆☆ (0) | 2023.09.14 |