반응형

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

 

14916번: 거스름돈

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

www.acmicpc.net

 

해결 방법 : 그리디

  • 최대한 5원으로 채우되, 5원으로 채우고 남는 거스름돈이 홀수이면 5원을 하나 빼면 된다.
import sys
input = sys.stdin.readline

def solution(n):
    if n in [1, 3]: return -1

    return ((n - 5) // 5) + (((n % 5) + 5) // 2) if (n % 5) % 2 == 1 else (n // 5) + ((n % 5) // 2)

if __name__ == '__main__':
    n = int(input())
    result = solution(n)
    print(result)
반응형

+ Recent posts