2839 - 설탕 배달

solution

code

#include <iostream>
using namespace std;

int calc(int N)
{
    int x = 0;
    int y = 0;
    if(N % 3 == 0)
    {
        x = N / 3;
        while(x >= 5)
        {
            x -= 5;
            y += 3;
        }
        return x + y;
    }
    else if(N % 5 == 0) return N / 5;
    else return -1;
}

int main()
{
    int N;
    cin >> N;

    int result = calc(N);
    if(result != -1) cout << result << endl;
    else if(N-5 > 0)
    {
        result = calc(N-5);
        if(result != -1) cout << result + 1 << endl;
        else if(N-10 > 0)
        {
            result = calc(N-10);
            if(result != -1) cout << result + 2 << endl;
            else cout << -1 << endl;
        }
        else cout << -1 << endl;
    }
    else cout << -1 << endl;
    
    return 0;
}

ref

2839번: 설탕 배달