<?php
function tinhTienDien($kWh) {
$tienDien = 0;
if ($kWh > 400) {
$tienDien += ($kWh - 400) * 2.927;
$kWh = 400;
}
if ($kWh > 300) {
$tienDien += ($kWh - 300) * 2.834;
$kWh = 300;
}
if ($kWh > 200) {
$tienDien += ($kWh - 200) * 2.536;
$kWh = 200;
}
if ($kWh > 100) {
$tienDien += ($kWh - 100) * 2.014;
$kWh = 100;
}
if ($kWh > 50) {
$tienDien += ($kWh - 50) * 1.734;
$kWh = 50;
}
if ($kWh > 0) {
$tienDien += $kWh * 1.678;
}
return $tienDien;
}
// Nhập số kWh từ bàn phím
echo "Nhập số kWh tiêu thụ: ";
$kWh = trim(fgets(STDIN
)); // Nhận input từ người dùng $kWh = intval($kWh); // Chuyển thành số nguyên
// Tính tiền điện
$tongTien = tinhTienDien($kWh);
// Hiển thị kết quả
echo "Số tiền điện phải trả: " . number_format($tongTien, 2) . " VNĐ\n"; ?>
PD9waHAKZnVuY3Rpb24gdGluaFRpZW5EaWVuKCRrV2gpIHsKICAgICR0aWVuRGllbiA9IDA7CiAgICAKICAgIGlmICgka1doID4gNDAwKSB7CiAgICAgICAgJHRpZW5EaWVuICs9ICgka1doIC0gNDAwKSAqIDIuOTI3OwogICAgICAgICRrV2ggPSA0MDA7CiAgICB9CiAgICBpZiAoJGtXaCA+IDMwMCkgewogICAgICAgICR0aWVuRGllbiArPSAoJGtXaCAtIDMwMCkgKiAyLjgzNDsKICAgICAgICAka1doID0gMzAwOwogICAgfQogICAgaWYgKCRrV2ggPiAyMDApIHsKICAgICAgICAkdGllbkRpZW4gKz0gKCRrV2ggLSAyMDApICogMi41MzY7CiAgICAgICAgJGtXaCA9IDIwMDsKICAgIH0KICAgIGlmICgka1doID4gMTAwKSB7CiAgICAgICAgJHRpZW5EaWVuICs9ICgka1doIC0gMTAwKSAqIDIuMDE0OwogICAgICAgICRrV2ggPSAxMDA7CiAgICB9CiAgICBpZiAoJGtXaCA+IDUwKSB7CiAgICAgICAgJHRpZW5EaWVuIC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