백준

[백준] 2839 설탕 배달_ JAVA

채니◟( ˘ ³˘)◞ 2023. 7. 3. 23:02

● 문제


● 풀이

 

봉지는 3kg, 5kg가 있고, 봉지의 최소 개수를 출력해야 하는 알고리즘이다.

 

5kg로 최대한 들고가고 남은 것을 3kg로 들고가면 된다.

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int cnt = 0;

        while (true) {
            if (N % 5 == 0) {
                System.out.println((N / 5) + cnt);
                break;
            } else {
                N -= 3;
                cnt++;
            }
            //4 or 7 같은 경우
            if (N < 0) {
                System.out.println(-1);
                break;
            }
        }
    }
}