이제하네 2024. 6. 24. 00:24

코딩테스트 연습 - 하샤드 수 | 프로그래머스 스쿨 (programmers.co.kr)

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

class Solution {
    public boolean solution(int x) {
        boolean answer = true;
        
        //int 를 Strinig 형변환
        String k = Integer.toString(x);
        
        //String 배열로 변환
        String[] arr = k.split("");
        
        //각 자리수의 합을 구할 변수 작성
        int total = 0;
        
        //각 자리의 합을 하나씩 더함(String이기 때문에 int로 변환)
        for(int i=0; i<arr.length; i++){
            total = total + Integer.parseInt(arr[i]);
        }
        
        if(x % total !=0){
            answer =false;
        }
        return answer;
    }
}