PAT-A1002 A+B for Polynomials (25分)

The original title

This time, you are supposed to find A+B where A and B are two
polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines,
and each line contains the information of a polynomial:
K N1 a​N​1​​ N​2 aN ​2 ​​ … N​K a ​NK

where K is the number of nonzero terms in the polynomial, Niand
aNi ​ (i=1,2,⋯,K) are the exponents and coefficients,
respectively. It is given that 1≤K≤10,0≤NK<⋯<N2 ​​ <N1 ​​≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line,
with the same format as the input. Notice that there must be NO extra
space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2 2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

classification

An array of maps (map or custom hash array)

Explain the meaning of problems

And calculating two polynomials

Ideas analysis

  1. And calculating the polynomials, the coefficients of the same order terms are added to
  2. Title given two polynomials, the first number in each input k denotes the number of non-zero coefficient term, and then a coefficient k of the index. Results in claim 1 decimal place.
  3. Note several points:
  • When mutually opposite number with the number of items of coefficient, the final result is not output
  • After the addition result is the same as the coefficient 0, the output is "0" (there is no longer any output later)

Reference Code

Method 1: The coefficients of the map storage, and recording of the results with a non-zero cnt

#include <cstdio>
#include <map>
using namespace std;
map <int, double> res;
int main(){
    int k, ex;
    double co;
    scanf("%d", &k);
    //cnt 记录结果中非零项个数
    int cnt = 0;
    for (int i = 0; i < k ; ++i) {
        scanf("%d%lf", &ex, &co);
        res[ex] = co;
        cnt++;
    }
    scanf("%d", &k);
    for (int i = 0; i < k ; ++i) {
        scanf("%d%lf", &ex, &co);
        //第一个多项式中无对应次数项,则结果中的非零项自增1
        if(res[ex] == 0) cnt++;
        res[ex] += co;
        //两非零项相加结果为0,则结果中的非零项自减1
        if(res[ex] == 0) cnt--;
    }
    printf("%d", cnt);
    if(cnt == 0){
        return 0 ;
    }
    auto it = res.end();
    while(it != res.begin()){
        it--;
        if(it->second!=0){
            printf(" %d %.1lf", it->first, it->second);
        }
    }
    return 0;
}

Method two: with the same coefficient memory map, but not record the results of nonzero coefficients in cnt

#include <cstdio>
#include <map>

using namespace std;
map<int, double> res;

int main() {
    int k, ex;
    double co;
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
        scanf("%d%lf", &ex, &co);
        res[ex] = co;
    }
    scanf("%d", &k);
    for (int i = 0; i < k; ++i) {
        scanf("%d%lf", &ex, &co);
        res[ex] += co;
        //相加结果为0时直接移除对应项
        if (res[ex] == 0) res.erase(ex);
    }
    printf("%d", res.size());
    if (res.size() == 0) {
        return 0;
    }
    auto it = res.end();
    while (it != res.begin()) {
        it--;
        if (it->second != 0) {
            printf(" %d %.1lf", it->first, it->second);
        }
    }
    return 0;
}

Method three: a hash may be stored in the coefficient array, similar to the method, which is not repeated herein.

Released five original articles · won praise 0 · Views 760

Guess you like

Origin blog.csdn.net/qq_36382924/article/details/104068194