#include <iostream> #include <string> #include <algorithm> using namespace std; string s; int sum = 0; int main() { cin >> s; while(1) { string tmp; while(s[0] != ',') { tmp += s[0]; s = s.substr(1); if(s.length() == 0) break; } sum += atoi(tmp.c_str()); //cout << tmp << " " << s << endl; if(s.length() == 0) break; s = s.substr(1); // 콤마 삭제 } cout << sum; }
string 클래스의 substr 사용법과 how to convert string to int만 알고있다면 쉽게 풀 수 있는 문제입니다.
<Convert string to int>
int n = atoi(str.c_str());
* string에 c_str()을 해주는 이유는 atoi 함수 안의 매개변수의 형식이 char*이여야 하기 때문에 변환해주어야 합니다.
'Algorithm > Baekjoon & Algospot' 카테고리의 다른 글
[C++] BOJ 7785 - 회사에 있는 사람 (0) | 2018.07.16 |
---|---|
[C++] BOJ 13016 - 내 왼손에는 흑염룡이 잠들어 있다 (0) | 2018.07.15 |
[C++] BOJ 2798 - 블랙잭 (0) | 2018.04.02 |
[C++] BOJ 2178 - 미로 탐색 (0) | 2018.03.28 |
[C++]BOJ 1157 - 단어 공부 (0) | 2018.03.23 |