
나의 풀이
class Solution {
public boolean solution(int x) {
boolean answer = true;
int firstNumber = x;
int sum = 0;
while(0 < x){
sum += x % 10;
x /= 10;
}
if(firstNumber % sum != 0){
answer = false;
}
return answer;
}
}
다른 사람의 풀이
class Solution {
public boolean solution(int x) {
boolean answer = true;
int sum = 0;
String[] num = Integer.toString(x).split("");
for(int i=0; i<num.length; i++) {
sum += Integer.parseInt(num[i]);
}
if(x % sum != 0)
answer = false;
return answer;
}
}
'알고리즘' 카테고리의 다른 글
| [프로그래머스] 콜라츠 추측(Java) * (0) | 2024.11.07 |
|---|---|
| [프로그래머스] 두 정수 사이의 합(Java) * (0) | 2024.11.06 |
| [프로그래머스] 정수 내림차순으로 배치하기(Java) * (0) | 2024.11.04 |
| [프로그래머스] 정수 제곱근 판별(Java) (0) | 2024.11.04 |
| [프로그래머스] 문자열을 정수로 바꾸기(Java) (0) | 2024.11.02 |