[백준 18330] Petrol (C#, C++) - soo:bak
작성일 :
문제 링크
설명
다음달 휘발유 사용에 대한 총 비용을 구하는 문제입니다.
접근법
다음달 시작 시 60L가 추가되므로 저가 구간은 60 + k 리터입니다.
저가 구간 내 사용량은 리터당 1500, 초과분은 리터당 3000으로 계산합니다.
사용량이 저가 한도 이하인지 확인하여 각각의 비용을 계산한 뒤 합산합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
class Program {
static void Main() {
var n = int.Parse(Console.ReadLine()!);
var k = int.Parse(Console.ReadLine()!);
var cheapLimit = 60 + k;
var cheapUse = Math.Min(n, cheapLimit);
var expensiveUse = Math.Max(0, n - cheapLimit);
var cost = cheapUse * 1500 + expensiveUse * 3000;
Console.WriteLine(cost);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
int cheapLimit = 60 + k;
int cheapUse = min(n, cheapLimit);
int expensiveUse = max(0, n - cheapLimit);
int cost = cheapUse * 1500 + expensiveUse * 3000;
cout << cost << "\n";
return 0;
}