求L题数据验证

RT
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string str;

int main() {
    cin >> str;
    double a = 0, b = 0;
    bool f = false;
    string ans;
    for (int i = 0, len = str.size(); i < len;) {
        if (str[i] == '=') {
            f = true;
            ++i;
            continue;
        }
        ll x = 0;
        bool unk = false, sign = true;
        if (str[i] == '+' || str[i] == '-') {
            sign = str[i] == '+';
            ++i;
        }
        bool vis = false;
        while (i < len && str[i] >= '0' && str[i] <= '9') {
            vis = true;
            x = x * 10 + str[i] - '0';
            ++i;
        }
        if (i < len && str[i] >= 'a' && str[i] <= 'z') {
            unk = true;
            string t;
            while (i < len && str[i] >= 'a' && str[i] <= 'z') {
                t.push_back(str[i]);
                ++i;
            }
            ans = t;
        }
        if (unk && !vis)
            x = 1;
        if (!sign)
            x = -x;
        // cout << unk << ";" << sign << ";" << x << endl;
        if (unk)
            b += !f ? x : -x;
        else
            a += f ? x : -x;
        // cout << a << ":" << b << endl;
    }
    cout << ans;
    printf("=%.3f\n", a / b);
    return 0;
}


全部评论
你没判断-0.000
2 回复 分享
发布于 2022-08-07 20:16

相关推荐

点赞 评论 收藏
分享
双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务