728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12947
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
public class Solution27_5 {
public static boolean solution(int x) {
boolean answer = true;
int origin = x;
int sum = 0;
while (origin != 0) {
sum += origin % 10;
origin /= 10;
}
if (x % sum != 0) {
answer = false;
}
return answer;
}
public static void main(String[] args) {
int x = 10;
System.out.println(solution(x));
int x1 = 12;
System.out.println(solution(x1));
int x2 = 11;
System.out.println(solution(x2));
int x3 = 13;
System.out.println(solution(x3));
}
}
처음 값을 origin에 저장해두고
자릿수를 바꿔가면서 더한다.
origin /=10;이 맞는데
origin %=10;을 하는 실수를 하였다.(실력)
728x90
'Java 알고리즘 공부 (프로그래머스)' 카테고리의 다른 글
| [프로그래머스 Lv1] x만큼 간격이 있는 n개의 숫자 (Java) (0) | 2023.10.03 |
|---|---|
| [프로그래머스 Lv1] 핸드폰 번호 가리기 (Java) (0) | 2023.10.03 |
| [프로그래머스 Lv1] 콜라츠 추측 (Java) (0) | 2023.10.03 |
| [프로그래머스 Lv1] 정수 제곱근 판별 (Java) (1) | 2023.09.17 |
| [프로그래머스 Lv1] 이상한 문자 만들기 (Java) (1) | 2023.09.17 |