Algorithm/[BOJ] - JAVA
[백준] 2231 : 분해합 JAVA 풀이
Codew
2022. 5. 24. 18:01
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ArrayEx1{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
/*
0부터 N보다 작은 수의 분해합을 구함
분해합이 N과 같으면 출력하고 반복문 탈출함
반복문이 끝까지 실행됐으면
생성자가 없다는 뜻이므로 0 출력
*/
for(int i=0;i<N;i++){
int sum = func(i);
if(sum==N){
System.out.println(i);
break;
}
if(i==N-1) {
System.out.println(0);
}
}
}
/*
분해합을 구하는 함수
분해합은 나 자신 + 각 자릿수의 합
*/
public static int func(int num){
int sum = num;
while(num!=0){
sum = sum+(num%10);
num = num / 10;
}
return sum;
}
}