[boj] (b4) 10162 전자레인지

강신현·2022년 4월 1일
0

✅ 그리디

문제

링크

풀이

버튼을 최소한으로 눌러야 하므로 가장 시간이 큰 버튼부터 눌러야한다.
-> 그리디 알고리즘

코드

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int A = 5 * 60, B = 60, C = 10, T, a = 0, b = 0, c = 0;
    cin >> T;

    while (1)
    {
        if (T >= A)
        {
            a += T / A;
            T = T % A;
        }
        else if (T >= B)
        {
            b += T / B;
            T = T % B;
        }
        else
        {
            if (T % C != 0) // 3개의 버튼으로 시간을 정확히 맞출 수 없는 경우
            {
                cout << "-1"
                     << "\n";
                return 0;
            }
            c += T / C;
            break;
        }
    }

    cout << a << " " << b << " " << c << "\n";
    return 0;
}
profile
땅콩의 모험 (server)

0개의 댓글