竞赛讨论区 > 求L题数据验证
头像
silenceneo
发布于 2022-08-07 17:15
+ 关注

求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;
}


全部评论

(1) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐