작성일 :

문제 링크

14470번 - 전자레인지

설명

초기 온도 A에서 목표 온도 B까지 전자레인지로 데우는 데 걸리는 총 시간을 구하는 문제입니다.


접근법

먼저 온도 구간별로 가열 시간이 다릅니다.

0℃ 미만일 때는 1℃ 올리는 데 C초가 걸리고, 0℃에서 해동하는 데 D초가 걸리며, 0℃ 초과일 때는 1℃ 올리는 데 E초가 걸립니다.

따라서 A가 0보다 작으면 0℃까지 올리는 시간, 해동 시간, B℃까지 올리는 시간을 모두 더합니다.

A가 0보다 크면 이미 해동된 상태이므로 B - A도를 E초씩 가열한 시간만 계산합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program {
  static void Main() {
    var a = int.Parse(Console.ReadLine()!);
    var b = int.Parse(Console.ReadLine()!);
    var c = int.Parse(Console.ReadLine()!);
    var d = int.Parse(Console.ReadLine()!);
    var e = int.Parse(Console.ReadLine()!);

    int ans;
    if (a < 0) ans = (-a) * c + d + b * e;
    else ans = (b - a) * e;

    Console.WriteLine(ans);
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;

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

  int a, b, c, d, e; cin >> a >> b >> c >> d >> e;
  int ans;
  if (a < 0) ans = (-a) * c + d + b * e;
  else ans = (b - a) * e;
  cout << ans << "\n";

  return 0;
}