작성일 :

문제 링크

10707번 - 수도요금

설명

두 수도 회사 X사Y사의 요금 체계가 다르게 주어졌을 때,

사용량에 따른 각 회사의 청구 요금을 비교하여 더 저렴한 쪽을 선택하는 문제입니다.

각 회사의 요금 체계

  • X사: 리터당 요금 A원이므로, 총 요금은 A × 사용량
  • Y사:
    • 기본 요금은 B
    • 사용량이 C리터 이하일 경우, 추가 요금 없음
    • 사용량이 C리터를 초과하면 초과분에 대해 1리터당 D원의 추가 요금 발생


접근법

  • 입력으로 주어진 요금 정보와 사용량을 변수로 저장합니다.
  • X사의 총 요금은 A × 사용량
  • Y사의 총 요금은 사용량이 C를 넘는 경우: B + (사용량 - C) × D, 그렇지 않으면 기본 요금 B
  • 두 요금 중 작은 값을 출력합니다.


Code

C#

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

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

    int costX = a * p;
    int costY = b + (p > c ? (p - c) * d : 0);

    Console.WriteLine(Math.Min(costX, costY));
  }
}

C++

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

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

  int xLiterRate, yBaseFee, yLimit, yExtraRate, waterUsage;
  cin >> xLiterRate >> yBaseFee >> yLimit >> yExtraRate >> waterUsage;

  int xTotalFee = xLiterRate * waterUsage;
  int yTotalFee = yBaseFee + (waterUsage > yLimit ? (waterUsage - yLimit) * yExtraRate : 0);

  cout << min(xTotalFee, yTotalFee) << "\n";

  return 0;
}