알고리즘
[백준] 2231 : 분해합 <JAVA>
HBean_
2021. 1. 13. 02:19
|
|
구현 순서 :
- 자연수 N 입력
- 자연수 1 ~ N-1까지 N의 생성자 인지 검사
- buf = N - test_N (test_N은 1부터 N까지 검사하는 수)
- buf와 test_N의 각 자릿수 합이 동일한지 검사
- 동일하다면 test_N 출력 -> 프로그램 종료
- 다르다면 2-1로 돌아가 test_N+1 검사
- 검사해도 찾지 못할 경우, 0 출력
- 종료
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int buf,check,sum;
for(int test_N=1; test_N < N ; test_N++){
buf = N - test_N;
sum = 0;
check = test_N;
while(check != 0){
sum += check%10;
check /= 10;
}
if(sum == buf){
System.out.println(test_N);
return;
}
}
System.out.println(0);
}
}