알고리즘(BOJ)/Silver

백준 - 거스름돈 14916

ch.0 2023. 2. 13. 20:10


문제 분석

거스름돈의 홀/짝 부터 분리했다. 그 이유는 홀수일경우 5의 경우도 홀수일수 밖에없다. 17 -> 5가 3개     11 -> 5가 1개

5보다 작을경우만 예외처리를 했다.

 

 

 

 

전체 소스

package test;

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

public class Main {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int input = Integer.parseInt(br.readLine());
		int five = 0;
		int two = 0;
		int result = 0;

		if (input < 5 && input % 2 == 1) {
			result=-1;

		} else if (input % 2 == 1) {
			result = input / 5;
			if (result % 2 == 0) {
				result--;
			}
			input = input - (5 * result);
			result = result + (input / 2);
		} else if (input % 2 == 0) {
			result = input / 5;
			if (result % 2 == 1) {
				result--;
			}
			input = input - (5 * result);
			result = result + (input / 2);
		}

		System.out.println(result);

	}

}

 


https://www.acmicpc.net/problem/14916

 

14916번: 거스름돈

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

www.acmicpc.net