1 条题解

  • 0
    @ 1988-12-2 15:51:28

    【深基3.习4】小玉家的电费

    解题思路

    前 150 度按 0.4463 元,接着至多 250 度按 0.4663 元,超过 400 度的部分按 0.5663 元,分别计算后相加。

    正确性说明

    阶梯电价按各段电量单独计费,三个区间不重叠且覆盖全部用电量,所以分段费用之和就是总费用。

    易错点

    不能将全部电量乘最高一档价格;输出一位小数。

    复杂度

    时间 O(1),空间 O(1)。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n;
        cin >> n;
        double ans = min(n, 150) * 0.4463 + min(max(n - 150, 0), 250) * 0.4663 +
                     max(n - 400, 0) * 0.5663;
        cout << fixed << setprecision(1) << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    49
    时间
    1000ms
    内存
    125MiB
    难度
    进阶
    标签
    递交数
    1
    已通过
    1
    上传者