Algorithm/C++ - 프로그래머스

프로그래머스 - 자릿수 더하기 C++

ㅇㅇ잉 2021. 2. 10. 14:25

string으로 바꿔서 문자 '0'을 빼주면 숫자로 변환할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;
int solution(int n)
{
    int answer = 0;
 
    string tmp=to_string(n);
    for(int i=0;i<tmp.length();i++){
        answer+=tmp[i]-'0';
    }
    return answer;
}
cs