1 条题解

  • 0
    @ 2026-10-27 12:58:43

    思路与算法

    定义三数最大值函数。把a,b,c分别乘1000存为整数A,B,C,计算U=max(A,B,C)、V=max(A+B,B,C)、W=max(A,B,B+C)。原式m=1000U/(VW),要输出的千分位整数是1000000U/(VW)按half-up舍入;用整数商和余数完成,避免二进制浮点中点误差。

    正确性说明

    正比例缩放保持大小关系,因此三个整数最大值恰对应原式三个最大值。代入比例得到m=1000U/(VW)。取1000m的整数商q、余数r:2r<VW时最近整数是q,否则half-up取q+1。拆成整数部分与补足三位的小数部分,恰是题目规定结果。

    复杂度

    数值位数受给定范围限制,初始化、三个max、乘除与输出都是O(1)次算术,额外空间O(1);十进制读取与输入文本长度成正比。

    易错点

    不是max(a,b,c)重复三次;两个分母的表达式不同。输入全正保证分母非零。分母最大4×10^12,使用64位整数;余数比较用2r,三位小数必须补前导0。

    参考代码(C++11)

    #include <bits/stdc++.h>
    using namespace std;
    long long scaled(string s) {
        if (s[0] == '+')
            s.erase(s.begin());
        auto dot = s.find('.');
        if (dot == string::npos)
            return stoll(s) * 1000;
        string tail = s.substr(dot + 1);
        if (tail.size() > 3)
            tail.resize(3);
        while (tail.size() < 3)
            tail += '0';
        return (dot ? stoll(s.substr(0, dot)) : 0) * 1000 + stoll(tail);
    }
    long long maximum(long long a, long long b, long long c) {
        return max(a, max(b, c));
    }
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        string x, y, z;
        cin >> x >> y >> z;
        long long a = scaled(x), b = scaled(y), c = scaled(z), u = maximum(a, b, c),
                  v = maximum(a + b, b, c), w = maximum(a, b, b + c);
        long long denominator = v * w, numerator = u * 1000000;
        long long ans = numerator / denominator;
        if ((numerator % denominator) * 2 >= denominator)
            ++ans;
        cout << ans / 1000 << '.' << setw(3) << setfill('0') << ans % 1000 << '\n';
    }
    
    • 1

    信息

    ID
    2070
    时间
    1000ms
    内存
    64MiB
    难度
    进阶
    标签
    递交数
    0
    已通过
    0
    上传者