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

문제

 

 

반응형

풀이

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Solution {
    public int solution(int n) {
        int answer = 0;
 
        int length = (int)(Math.log10(n)+1);                //n 의 자릿수
        
        for(int i=0; i<length; i++) {
            answer+=n%Math.pow(10, i+1)/Math.pow(10, i);    //10의 n승만큼 계속 나누면서 각 자릿수를 더함
        }
 
        return answer;
    }
}
cs

 

728x90
반응형