hahn

[백준 - JAVA] 거스름돈 본문

카테고리 없음

[백준 - JAVA] 거스름돈

hahn 2021. 9. 17. 11:22
728x90
반응형

14916번: 거스름돈 (acmicpc.net)

 

14916번: 거스름돈

첫째 줄에 거스름돈 액수 n(1 ≤ n ≤ 100,000)이 주어진다.

www.acmicpc.net

http://boj.kr/eeb59436bade4a7c806927d5f1f5ba95

 

공유 소스 보기

 

www.acmicpc.net

더보기
import java.util.Scanner;

class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
		
		int input, result = 0;
		
		input = sc.nextInt();
		
		while(input != 0) {
			
			if(input < 2) {
				
				result = -1;
				break;
				
			}
			
			if((input - 5) % 2 == 0 || input / 5 >= 2) {
				
				input -= 5;
				result++;
				
			}else {
				
				input -= 2;
				result++;
				
			}
			
			
		}
		
		System.out.println(result);
        
    }
    
}

일단 1원이 되면 거슬러줄 수 없으니 -1 반환

 

9원 이하 일 때,

 

5원을 거슬러주고 2로 나누어 떨어져 지지 않으면

 

1원이 남게되므로 위 조건 추가

 

if((input - 5) % 2 == 0 || input / 5 >= 2)

 

실수로 >=를 >로 써서 틀렸었다..

728x90
반응형