티스토리 뷰
[BJ] 2231. 분해합
2231번: 분해합
어떤 자연수 N이 있을 때, 그 자연수 N의 분해합은 N과 N을 이루는 각 자리수의 합을 의미한다. 어떤 자연수 M의 분해합이 N인 경우, M을 N의 생성자라 한다. 예를 들어, 245의 분해합은 256(=245+2+4+5)이
www.acmicpc.net
import java.io.*;
import java.util.*;
// 210211
public class Main_BJ_2231_분해합 {
static int N;
static int func(int num) {
String str = Integer.toString(num);
int res = 0;
for(int i=0; i<str.length(); i++){
res += str.charAt(i) - '0';
}
return res+num;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
int input = 1;
while(input<N) {
int output = func(input);
if (output==N) break;
else ++input;
}
// 분해합이 없는 경우 0을 출력한다.
System.out.println((input==N) ? 0 : input);
br.close();
}
}
'코딩테스트 > 백준' 카테고리의 다른 글
[BJ] 1018. 체스판 다시 칠하기 (0) | 2021.02.14 |
---|---|
[BJ] 7568. 덩치 (0) | 2021.02.14 |
[BJ] 2798. 블랙잭 (0) | 2021.02.14 |
[BJ] 11729. 하노이 탑 이동 순서 (0) | 2021.02.14 |
[BJ] 10870. 피보나치 수 5 (0) | 2021.02.14 |
댓글