
1. 문제 풀이 아이디어
a + b < 2c
이면 전부 개별로 구매하는 것이 이득이므로 단순 계산하고, 그 외에는c
를x
와y
중 작은 값부터 큰 값까지 계산하여 문제를 해결할 수 있다.
- 치킨의 개수를 x와 y에 맞추는 것이 아니라 그 이상도 구매할 수 있다.
2. 나의 정답 코드
using (StreamReader sr = new StreamReader(Console.OpenStandardInput()))
using (StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()))
{
int[] input = Array.ConvertAll(sr.ReadLine().Trim().Split(), int.Parse);
int a = input[0];
int b = input[1];
int c = input[2];
int x = input[3];
int y = input[4];
int min = Math.Min(x, y);
int max = Math.Max(x, y);
int result = int.MaxValue;
if (a + b < c * 2)
{
result = a * x + b * y;
}
else
{
for (int i = min; i <= max; i++)
{
int cal = a * Math.Max(0, (x - i)) + b * Math.Max(0, (y - i)) + c * i * 2;
if (cal > result) break;
result = Math.Min(cal, result);
}
}
sw.WriteLine(result);
}
3. 정리
a + b < c * 2
조건이면 반반 치킨을 살 이유가 없으므로 단순 계산한다.
- 그렇지 않으면, x와 y중 작은 값에서 큰 값까지 순회하며 계산된 값을 구한다.
- 계산된 값이 이전 값보다 클 경우 반복문을 빠져나온다.
- 최종 계산된 결과를 출력하여 문제를 해결한다.
Share article