백준/실버

[백준 14916번] 거스름돈 (C++)

게임개발기원 2023. 6. 18. 16:22

문제링크 : https://www.acmicpc.net/problem/14916

 

14916번: 거스름돈

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

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = 987654321;

int main()
{
    ios_base::sync_with_stdio(0); 
	cin.tie(0);

    int N;
    cin >> N;

    if(N==1 || N==3) cout << -1; //불가능
    else if(N%5%2 == 0) cout << N/5 + N%5/2; //짝수
    else cout << N/5-1+ (N%5+5) / 2; //홀수

    return 0;
}

그리디 문제다.

N이 1과 3일때는 계산이 불가하므로 -1을 출력하고 

이후에 짝수의 경우와 홀수의 경우를 판별하여 값을 출력한다.