작성일 :

문제 링크

11296번 - 가격

설명

색 스티커 할인과 쿠폰 할인 후, 결제 방식의 반올림 규칙에 맞춰 가격을 출력하는 문제입니다.


접근법

색상별 할인율을 맵에 저장해 두고, 원래 가격에 해당 할인율을 적용합니다.

쿠폰이 있으면 추가로 5%를 할인하고, 매 계산 후 소수점 둘째 자리까지 반올림합니다.

현금 결제 시에는 소수점 둘째 자리가 6 이상이면 반올림하고, 아니면 내림합니다.


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
33
34
35
36
37
using System;
using System.Collections.Generic;

class Program {
  static void Main() {
    var dots = new Dictionary<string, int> {
      {"R", 45}, {"G", 30}, {"B", 20},
      {"Y", 15}, {"O", 10}, {"W", 5}
    };

    var n = int.Parse(Console.ReadLine()!);
    for (var i = 0; i < n; i++) {
      var parts = Console.ReadLine()!.Split();
      var originalPrice = double.Parse(parts[0]);
      var color = parts[1];
      var coupon = parts[2];
      var payment = parts[3];

      var currentPrice = originalPrice;

      currentPrice -= currentPrice * dots[color] / 100;
      currentPrice = Math.Round(currentPrice * 100) / 100.0;

      if (coupon == "C") currentPrice -= currentPrice * 5 / 100;

      currentPrice = Math.Round(currentPrice * 100) / 100.0;

      if (payment == "C") {
        var secondDecimal = (int)(currentPrice * 100) % 10;
        if (secondDecimal >= 6) currentPrice = Math.Round(currentPrice);
        else currentPrice = (currentPrice * 100 - secondDecimal) / 100.0;
      }

      Console.WriteLine($"${currentPrice:F2}");
    }
  }
}

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
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;

typedef unordered_map<string, int> umsi;

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

  umsi dots = {
    {"R", 45}, {"G", 30}, {"B", 20},
    {"Y", 15}, {"O", 10}, {"W", 5}
  };

  int n; cin >> n;

  cout << fixed << setprecision(2);
  for (int i = 0; i < n; i++) {
    double originalPrice;
    string color, coupon, payment;
    cin >> originalPrice >> color >> coupon >> payment;

    double currentPrice = originalPrice;

    currentPrice -= currentPrice * dots[color] / 100;
    currentPrice = round(currentPrice * 100) / 100.0;

    if (coupon == "C") currentPrice -= currentPrice * 5 / 100;

    currentPrice = round(currentPrice * 100) / 100.0;

    if (payment == "C") {
      int secondDecimal = static_cast<int>(currentPrice * 100) % 10;
      if (secondDecimal >= 6) currentPrice = round(currentPrice);
      else currentPrice = (currentPrice * 100 - secondDecimal) / 100.0;
    }

    cout << "$" << currentPrice << "\n";
  }

  return 0;
}