반응형
문제 |
소스 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Queue<Integer> q = new LinkedList<Integer>();
int[] answer = new int[N+1];
int val = 0;
if(N>=5) {
q.add(5);
answer[5] = 1;
}
if(N>=3) {
q.add(3);
answer[3] = 1;
}
while(!q.isEmpty()) {
val = q.poll();
if(val == N) {
break;
}
if(val+5 <= N && answer[val+5] == 0) {
q.add(val+5);
answer[val+5] = answer[val]+1;
}
if(val+3 <= N && answer[val+3] == 0) {
q.add(val+3);
answer[val+3] = answer[val]+1;
}
}
System.out.println(answer[N] == 0 ? -1 : answer[N]);
}
}
출처 |
https://www.acmicpc.net/problem/2839
반응형
'알고리즘 > JAVA' 카테고리의 다른 글
| [JAVA] 백준 알고리즘 1149 RGB거리 (0) | 2019.04.23 |
|---|---|
| [JAVA] 백준 알고리즘 2293 : 동전 1 (DP) (0) | 2019.03.31 |
| [JAVA] 백준 알고리즘 2156 : 포도주 (DP) (0) | 2018.10.11 |
| [JAVA] 백준 알고리즘 1912 : 연속합 (DP) (0) | 2018.10.10 |
| [JAVA] 백준 알고리즘 1920 : 수 찾기 (0) | 2018.09.20 |