본문으로 바로가기
728x90
반응형

문제

 

 

반응형

 

풀이

 

 

 

1
2
3
4
5
6
7
8
9
10
class Solution {
    public int solution(int n) {
        int answer = 0;
        for(int i=(int) (Math.log10(n)); i>=0; i--) {     //log를 사용해서 자릿수를 구함
            answer+=n%10;                                 //n을 10으로 나누어 각 자릿수를 answer에 더함
            n/=10;                                        //n을 10으로 나누어 자릿수를 하나씩 줄임
        }
        return answer;
    }
}
cs

 

728x90
반응형