[백준 16917] 양념 반 후라이드 반 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
양념과 후라이드를 최소 수량 이상 구매할 때 비용의 최솟값을 구하는 문제입니다.
접근법
반반 치킨 2마리로 양념 1마리와 후라이드 1마리를 대체할 수 있습니다.
반반 개수를 0부터 필요한 수량의 두 배까지 늘려가며 최소 비용을 갱신합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
class Program {
static void Main() {
var parts = Console.ReadLine()!.Split();
var a = int.Parse(parts[0]);
var b = int.Parse(parts[1]);
var c = int.Parse(parts[2]);
var x = int.Parse(parts[3]);
var y = int.Parse(parts[4]);
var best = int.MaxValue;
var limit = Math.Max(x, y) * 2;
for (var i = 0; i <= limit; i += 2) {
var needA = x - i / 2;
var needB = y - i / 2;
if (needA < 0) needA = 0;
if (needB < 0) needB = 0;
var cost = i * c + needA * a + needB * b;
if (cost < best) best = cost;
}
Console.WriteLine(best);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b, c, x, y;
cin >> a >> b >> c >> x >> y;
int best = INT_MAX;
int limit = max(x, y) * 2;
for (int i = 0; i <= limit; i += 2) {
int needA = x - i / 2;
int needB = y - i / 2;
if (needA < 0) needA = 0;
if (needB < 0) needB = 0;
int cost = i * c + needA * a + needB * b;
if (cost < best) best = cost;
}
cout << best << "\n";
return 0;
}