작성일 :

문제 링크

4727번 - Close Enough Computations

설명

표시된 지방, 탄수화물, 단백질 그램 수와 칼로리가 모두 반올림된 값이라고 할 때, 실제 값들이 그 표기를 만들 수 있는지 판단하는 문제입니다.


접근법

정수로 표시된 그램 수 g가 있다면, 실제 값은 반올림 전 기준으로 g - 0.5 이상 g + 0.5 미만 범위에 있어야 합니다. 다만 그램 수는 음수가 될 수 없으므로 0인 경우 하한은 0입니다.

지방은 1그램당 9칼로리, 탄수화물과 단백질은 1그램당 4칼로리이므로, 각 영양소의 가능한 최소값과 최대값으로 전체 칼로리의 가능한 구간도 바로 구할 수 있습니다.

이 칼로리 구간이 표시된 칼로리가 반올림될 수 있는 구간과 겹치면 yes, 겹치지 않으면 no입니다. 구현에서는 0.5를 없애기 위해 모든 값을 2배 해서 정수로 계산하면 편합니다.



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
27
28
29
30
31
32
using System;

class Program {
  static void Main() {
    while (true) {
      string[] input = Console.ReadLine()!.Split();
      int calories = int.Parse(input[0]);
      int fat = int.Parse(input[1]);
      int carb = int.Parse(input[2]);
      int protein = int.Parse(input[3]);

      if (calories == 0 && fat == 0 && carb == 0 && protein == 0)
        break;

      int fatLow = Math.Max(0, fat * 2 - 1);
      int fatHigh = fat * 2 + 1;
      int carbLow = Math.Max(0, carb * 2 - 1);
      int carbHigh = carb * 2 + 1;
      int proteinLow = Math.Max(0, protein * 2 - 1);
      int proteinHigh = protein * 2 + 1;

      int minCalories = 9 * fatLow + 4 * carbLow + 4 * proteinLow;
      int maxCalories = 9 * fatHigh + 4 * carbHigh + 4 * proteinHigh;

      int labelLow = Math.Max(0, calories * 2 - 1);
      int labelHigh = calories * 2 + 1;

      bool possible = Math.Max(minCalories, labelLow) < Math.Min(maxCalories, labelHigh);
      Console.WriteLine(possible ? "yes" : "no");
    }
  }
}

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
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while (true) {
    int calories, fat, carb, protein;
    cin >> calories >> fat >> carb >> protein;

    if (calories == 0 && fat == 0 && carb == 0 && protein == 0)
      break;

    int fatLow = max(0, fat * 2 - 1);
    int fatHigh = fat * 2 + 1;
    int carbLow = max(0, carb * 2 - 1);
    int carbHigh = carb * 2 + 1;
    int proteinLow = max(0, protein * 2 - 1);
    int proteinHigh = protein * 2 + 1;

    int minCalories = 9 * fatLow + 4 * carbLow + 4 * proteinLow;
    int maxCalories = 9 * fatHigh + 4 * carbHigh + 4 * proteinHigh;

    int labelLow = max(0, calories * 2 - 1);
    int labelHigh = calories * 2 + 1;

    bool possible = max(minCalories, labelLow) < min(maxCalories, labelHigh);
    cout << (possible ? "yes" : "no") << "\n";
  }

  return 0;
}

Tags: 4727, BOJ, C#, C++, 구현, 백준, 수학, 알고리즘

Categories: ,