#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*이여야 하기 때문에 변환해주어야 합니다.

+ Recent posts