1. 程式人生 > 其它 >L2-018 多項式A除以B

L2-018 多項式A除以B

#include <bits/stdc++.h>

using namespace std;

#define e first
#define c second
constexpr double eps = 1e-8;

int main() {
    int n;
    cin >> n;
    map<int, double> q;
    int mx;
    for (int i = 0; i < n; i++) {
        int e, c;
        cin >> e >> c;
        q[e] = c;
        if (i == 0) mx = e;
    }
    int m;
    cin >> m;
    vector<pair<int, double>> y(m);
    for (int i = 0; i < m; i++) {
        cin >> y[i].e >> y[i].c;
    }
    vector<pair<int, double>> res;
    while (mx >= y[0].e) {
        double change = q[mx] / y[0].c;
        int diff = mx - y[0].e;
        if (fabs(change) >= 0.05) {
            res.push_back({diff, change});
            for (int i = 0; i < m; i++) {
                q[y[i].e + diff] -= change * y[i].c;
            }

        } else mx--;
        while (mx >= y[0].e && fabs(q[mx]) < 0.05) mx--;
    }

    cout << res.size();
    if (res.empty()) cout << " 0 0.0";
    for (int i = 0; i < res.size(); i++) {
          printf(" %d %.1f", res[i].e, res[i].c);
    }
    puts("");
    res.clear();
    while (mx >= 0) {
        if (fabs(q[mx]) >= 0.05) {
            res.push_back({mx, q[mx]});
        }
        mx--;
    }
    cout << res.size();
    if (res.empty()) cout << " 0 0.0";
    for (int i = 0; i < res.size(); i++) {
        printf(" %d %.1f",res[i].e,res[i].c);
    }

    return 0;
}