알고리즘 풀이/백준

[백준][Java] 2231번 분해합

배게 2019. 2. 19. 14:03
728x90

입력받은 값 a에서부터 1씩 뺀 후에


그 값의 분해합이 처음에 주어진 값 a와 일치할 경우


res값을 갱신해준다


a-1부터 1이 될 때까지 while문을 돌리며


while문이 끝난 후의 res값이 문제에서 요구하는 가장 작은 생성자이다



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int a = sc.nextInt();
		int index = a;
		int res=0;
		
		while(index>0) {
			index--;
//			System.out.println(index);
			
			int comp=index;
			int unit=index; 
			while(unit!=0) {
				comp+=unit%10;
//				System.out.println("comp값 " + comp);
				unit/=10;
			}
			if(comp==a) res=index;
		}
		
		System.out.println(res);
		
	}
}