
[백준 14916] 거스름돈 JAVA
·
코딩테스트/백준
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n;//거스름돈 금액 int x = 0;//동전 개수 n = sc.nextInt(); while(n >= 5) { if(n == 8 || n == 6)//8과 6은 2로 거슬러줘야함 break; n-= 5; x++; } if(n % 2 == 0) { while(n >= 2) { n-=2; x++; } System.out.println(x); } else { System.out.println(-1); } } } 이번 문제는 나름 쉽게 푼 것 같다.