Solutions of Electricity bill - MarisaOJ: Marisa Online Judge

Solutions of Electricity bill

Select solution language

Write solution here.


User Avatar benjamin_tom    Created at    2 likes

# Khuyến khích các bạn tự suy nghĩ làm bài trước khi xem lời giải ## Ý tưởng chính: ### Nếu x trong đoạn [0; 50]: toàn bộ tiền điện sẽ tính theo a ``` Tiền điện = x * a ``` ### Nếu x trong đoạn [51; 100]: 50kWh đầu sẽ tính theo a, còn lại sẽ tính theo b ``` Tiền điện = 50*a + (x - 50)*b ``` ### Nếu x trong đoạn [101, 150]: 50kWh đầu sẽ tính theo a, 50kWh tiếp theo tính theo b, còn lại sẽ tính theo c ``` Tiền điện = 50*a + 50*b + (x - 100)*c ``` ### Nếu x > 150: 50kWh đầu sẽ tính theo a, 50kWh tiếp theo tính theo b, 50kWh tiếp theo tính theo c, còn lại sẽ tính theo d ``` Tiền điện = 50*a + 50*b + 50*c + (x - 150)*d ``` ## Code tham khảo: ``` #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); short x, a, b, c, d; cin >> x >> a >> b >> c >> d; int sum = 0; if(x <= 50) sum = x*a; else if(x <= 100) sum = 50*a + (x - 50)*b; else if(x <= 150) sum = 50*a + 50*b + (x - 100)*c; else sum = 50*a + 50*b + 50*c + (x - 150)*d; cout << sum; return 0; } ```