https://school.programmers.co.kr/learn/courses/30/lessons/12947

 

프로그래머스

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

programmers.co.kr


10으로 나누며 나머지를 더해서 합을 구하고 나누어 떨어지는가? 만 확인하면 된다.

 

- Java

class Solution {
    public boolean solution(int x) {
        int k=x;
        int a =0;
        while(x>0){
            a+=x%10;
            x/=10;
        }
        if(k%a==0){
            return true;
        }
        else return false;
    }
}

 

 

- Ruby

def solution(x)
    a=0
    b=x
    while b>0
        a=a+b%10
        b=b/10
    end
    if x%a==0
        return true
    else
        return false
    end
end

+ Recent posts