7-9 polynomial A divided by B (25 points)

This is still a question about A/B, but both A and B are replaced by polynomials. You need to calculate the quotient Q and remainder R of the division of two polynomials, where the order of R must be less than the order of B.

Input format: The
input is divided into two lines, each line gives a non-zero polynomial, first give A, then give B. The format of each line is as follows:

N e[1] c[1]… e[N] c[N]
where N is the number of non-zero terms in the polynomial, e[i] is the exponent of the i-th non-zero term, and c[i] is the The coefficients of i non-zero terms. The items are given in descending order of exponents, ensuring that all exponents are different non-negative integers, all coefficients are non-zero integers, and all integers are within the range of integers.

Output format:
output the quotient and remainder in two lines one after the other. The output format is the same as the input format, and the output coefficient retains one decimal place. The numbers in the same line are separated by 1 space, and there must be no extra spaces at the beginning and end of the line. Note: The zero polynomial is a special polynomial, and the corresponding output is 0 0 0.0. However, non-zero polynomials cannot output terms with zero coefficients (including 0.0 after rounding). In the example, the copolynomial actually has a constant term -1/27, but it is not output because it is rounded to 0.0.

Input sample:

4 4 1 2 -3 1 -1 0 -1
3 2 3 1 -2 0 1

Sample output:

3 2 0.3 1 0.2 0 -1.0
1 1 -3.1

Simulating polynomial division. In high school, the handsome math teacher showed us several times when solving problems. I didn’t care. I didn’t think it was necessary. Just listen to it. I was dumbfounded when I saw this problem. Baidu took a look at how it was calculated. This is just a bit of thinking. The final output always has problems. Note that the quotient and the remainder are output separately, the zero polynomial of the quotient, the output corresponds to 0 0 0.0, and the remainder corresponds to the remainder, don’t just output one. Whether it is regarded as 0 is to see two decimal places, just compare with 0.05, rounding up
Attach the calculation process, there should be no calculation error. .

Insert picture description here

Regarding the retention of decimal places, I have found that there is a problem with my previous understanding when I have been doing questions recently, so I have to test the examples myself and remember the results.

Insert picture description here

#include <cmath>
#include <iostream>
#include <map>
using namespace std;

int main() {
    
    
    int n1, n2;
    cin >> n1;
    map<int, double> m1, m2; // m1存储被除数(包括最后的余数),m2存储商
    int a[100000];           //存储除数的指数
    double b[100000]; //存储除数的系数
    int e, max1 = 0, max2 = 0;
    double c;
    for (int i = 0; i < n1; i++) {
    
    
        cin >> e >> c;
        m1[e] = c;
        if (i == 0)
            max1 = e;
    }
    cin >> n2;
    for (int i = 0; i < n2; i++) {
    
    
        cin >> a[i];    //除数的指数
        cin >> b[a[i]]; //除数的系数
        if (i == 0)
            max2 = a[i];
    }
    int i = max1;
    while (i >= max2) {
    
    
        int diff = i - a[0];
        m2[diff] = m1[i] / b[a[0]];
        for (int j = 0; j < n2; j++) {
    
    
            m1[diff + a[j]] -= m2[diff] * b[a[j]];
        }
        i--;
    }
    int cnt1 = 0, cnt2 = 0;
    for (auto it = m1.begin(); it != m1.end(); it++) {
    
    
        if (abs(it->second) < 0.05)
            it->second = 0;
        else
            cnt1++;
    }
    for (auto it = m2.begin(); it != m2.end(); it++) {
    
    
        if (abs(it->second) < 0.05)
            it->second = 0;
        else
            cnt2++;
    }
    if (cnt2 == 0)
        cout << "0 0 0.0";
    else {
    
    
        cout << cnt2;
        for (i = max1; i >= 0; i--) {
    
    
            if (m2[i])
                printf(" %d %.1lf", i, m2[i]);
        }
    }
    cout << endl;
    if (cnt1 == 0)
        cout << "0 0 0.0";
    else {
    
    
        cout << cnt1;
        for (i = max1; i >= 0; i--) {
    
    
            if (m1[i])
                printf(" %d %.1lf", i, m1[i]);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/112916512