CS/알고리즘
[백준/JAVA] 2331번 : 분해합
코딩 펭귄
2021. 9. 7. 19:41
https://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.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int result = 0;
for(int i = 1; i <= 1000000; i++) {
String strNum = Integer.toString(i);
int sum = i;
for(int j = 0; j < strNum.length(); j++) {
sum += Character.getNumericValue(strNum.charAt(j));
}
if(sum == num) {
result = i;
break;
}
}
System.out.println(result);
}
}