ALGORITHM

프로그래머스_카카오_데모1

서울소시민 2017. 9. 11. 14:06

문제 :

숫자 N을 입력받아서 각자리를 더해준 값 출력


int -> string -> char -> int

??

간결하지 않다...


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int solution(int n)
{
    int answer = 0;
    string tmp = to_string(n);
    for(int i=0;i<tmp.size();i++){
        answer+=tmp[i]-'0';
    }
    return answer;
}

int main(){
    return solution(123);
}


'ALGORITHM' 카테고리의 다른 글

프로그래머스_카카오_데모3  (0) 2017.09.13
[?]프로그래머스_카카오_데모2  (0) 2017.09.12
BFS_너비우선탐색  (0) 2017.09.10
[?]프로그래머스_level3_시저암호  (0) 2017.09.09
프로그래머스_level3_야근지수  (0) 2017.09.08