코딩테스트/백준

[BJ] 2231. 분해합

jhk828 2021. 2. 14. 03:16

[BJ] 2231. 분해합

www.acmicpc.net/problem/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();
	}
}