알고리즘/백준
99클럽 코테 스터디 14일차 TIL (14916번: 거스름돈)
Sondho
2024. 11. 11. 09:59
문제
https://www.acmicpc.net/problem/14916
학습 키워드
- 그리디
시도
- (성공) 5원으로 교환할 수 있는 최대값부터 시작해서 5원으로 교환하고 남은 돈을 2원으로 전부 교환할 수 있는지 판단하기
풀이 및 코드
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int answer = 0;
for (int fiveWon = n / 5; fiveWon >= 0; fiveWon--) {
int remainderN = n - (5 * fiveWon);
if (remainderN % 2 == 0) {
answer = fiveWon + (remainderN / 2);
break;
}
}
if (answer == 0) {
System.out.println(-1);
} else {
System.out.println(answer);
}
}
}