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