Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-2839번/Java] 설탕배달 본문

Algorithm

[백준-2839번/Java] 설탕배달

1984 2022. 11. 11. 11:39

* 다이나믹 프로그래밍

* 그리디 알고리즘

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

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);

		int sugar = sc.nextInt();

		int five_q = sugar / 5;
		int five_r = sugar % 5;

		int three_q = 0;

		while (true) {

			 if (five_r == 1) {
				five_q -= 1;
				three_q += 2;
			} else if (five_r == 2) {
				five_q -= 2;
				three_q += 4;
			} else if (five_r == 3) {
				three_q++;
			} else if (five_r == 4) {
				five_q -= 1;
				three_q += 3;
			}
			break;
		}

		if (five_q >= 0 && three_q >= 0) {
			System.out.println(five_q + three_q);
		} else {
			System.out.println(-1);
		}
		
		sc.close();
	}

}
728x90
Comments