Jun's Development Journey

[BOJ] 2839번 설탕 배달 본문

BOJ/Greedy

[BOJ] 2839번 설탕 배달

J_Jayce 2021. 3. 10. 13:36

문제

www.acmicpc.net/problem/2839

 

2839번: 설탕 배달

상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그

www.acmicpc.net

풀이

import java.io.*;
import java.util.*;

public class Main {
	
	static int N;
	static int get_package(int N) {
		ArrayList<Integer> cnts = new ArrayList<Integer>();
		int f_div = N/5, th_div = N/3;
		if(N%5==0)
			cnts.add(N/5);
		if(N%3==0)
			cnts.add(N/3);
		for(int i=1;i<=f_div;i++) {
			int tmp = N-(5*i);
			int cnt=i;
			if(tmp%3==0)
				cnt+=tmp/3;
			tmp%=3;
			if(tmp==0)
				cnts.add(cnt);
		}
		Collections.sort(cnts);
		return cnts.isEmpty() ? -1 : cnts.get(0);
	}
	public static void main(String[] args) throws IOException {
        //선언 및 입력
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st;
    	StringBuilder sb = new StringBuilder();
    	N = Integer.parseInt(br.readLine());
    	
    	//출력
    	System.out.println(get_package(N));
	}
} 

'BOJ > Greedy' 카테고리의 다른 글

[BOJ] 15729번 방탈출  (0) 2021.03.24
[BOJ] 2810번 컵홀더  (0) 2021.03.10
[BOJ] 2965번 캥거루 세마리  (0) 2021.03.09
[BOJ] 1434번 책정리  (0) 2021.03.09
[BOJ] 5585번 거스름돈  (0) 2021.03.09