알고리즘/문제풀이
[백준] 2231. 분해합
BSHwan
2020. 1. 26. 10:49
반응형
https://www.acmicpc.net/problem/2231
2231번: 분해합
문제 어떤 자연수 N이 있을 때, 그 자연수 N의 분해합은 N과 N을 이루는 각 자리수의 합을 의미한다. 어떤 자연수 M의 분해합이 N인 경우, M을 N의 생성자라 한다. 예를 들어, 245의 분해합은 256(=245+2+4+5)이 된다. 따라서 245는 256의 생성자가 된다. 물론, 어떤 자연수의 경우에는 생성자가 없을 수도 있다. 반대로, 생성자가 여러 개인 자연수도 있을 수 있다. 자연수 N이 주어졌을 때, N의 가장 작은 생성자를 구해내는 프로그
www.acmicpc.net
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int answer = 0;
for(int i=1; i<N; i++) {
int result = 0;
result += i;
String st = Integer.toString(i);
for(int j=0; j<st.length(); j++)
result += (st.charAt(j)-48);
if(result == N) {
answer =i;
break;
}
}
System.out.println(answer);
}
}
반응형