곽로그
[백준 2231 자바] 분해합 본문
반응형
문제
https://www.acmicpc.net/problem/2231
접근
포인트
코드
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int num = in.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
//최소시작점 구하기
int minStart = 0;
int temp = num;
while(temp>0) {
minStart +=9;
temp/=10;
}
minStart= num-minStart;
//생성자 구하기
for(int i=minStart; i < num;i++) {
int bunhaehap = i;
int temp2 = i;
while(temp2>0) {
bunhaehap += temp2%10;
temp2/=10;
}
if(bunhaehap == num) {
list.add(i);
}
}
//최솟값 출력
if(list.size()==0) {
System.out.println(0);
}
else {
int min = list.get(0);
for(int i=0; i<list.size();i++) {
if(list.get(i)<min) {
min = list.get(i);
}
}
System.out.println(min);
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = br.readLine().trim();
StringTokenizer st = new StringTokenizer(line);
int N = Integer.parseInt(st.nextToken());
findMaker(N);
if(makers.size()==0){
bw.write("0");
}
else{
Collections.sort(makers);
bw.write(String.valueOf(makers.get(0)));
}
bw.flush();
}
public static ArrayList<Integer> makers = new ArrayList<Integer>();
public static void findMaker(int N){
for(int num =1 ;num<=N ;num++){
int bunHaeHap = findBunHaeHap(num);
//System.out.println("num:"+num+"bunHaHap:"+bunHaeHap);
if(bunHaeHap==N){
makers.add(num);
}
}
}
private static int findBunHaeHap(int num){
int bunHaeHap = num;
while(num>0){
bunHaeHap+=num%10;
num=num/10;
}
return bunHaeHap;
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준 2789 자바] 블랙잭 (0) | 2020.03.08 |
---|---|
[백준 1018 자바] 체스판 다시 칠하기 (0) | 2020.03.07 |
[백준 7568] 덩치 (0) | 2020.03.02 |
[백준 1157] 단어공부 - 다시 (0) | 2020.03.01 |
[백준 2675] 문자열 반복 (0) | 2020.03.01 |
Comments