# 题目描述

题目链接

This time, you are supposed to find A+BA+B where AA and BB 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:

KN1aN1N2aN2...NKaNKK N_1 a_{N_1} N_2 a_{N_2}...N_Ka_{N_K} where KK is the number of nonzero terms in the polynomial, NiN_i and aNi(i=1,2,,K)a_{N_i}(i=1,2,\cdots ,K) are the exponents and coefficients, respectively. It is given that 1K10,0NK<<N2<N110001≤K≤10, 0≤N_K<⋯< N_2< N_1≤1000.

# Output Specification:

For each test case you should output the sum of AA and BB 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

# 解释

给出两个关于 x 的多项式,计算它们的和。多项式的表示形式为:首先是项数,然后依次的整型和浮点数表示指数和系数,按指数从大向小排列。

# 一种思路

建立两个浮点数组存放两个多项式,下标相当于指数,值相当于系数。那么之后相加的时候只需要遍历下标然后直接求和即可。这里注意系数为 0 的项表示不存在,不要输出。

# 解答

#include <bits/stdc++.h>
using namespace std;
static double seq1[1005], seq2[1005];
int main(){
    int len, index, max = -1;
    cin >> len;
    for (int i = 0 ; i < len ; i++){
        cin >> index;
        cin >> seq1[index];
        if (index > max) max = index;
    }
    cin >> len;
    for (int i = 0 ; i < len ; i++){
        cin >> index;
        cin >> seq2[index];
        if (index > max) max = index;
    }
    for (int i = max; i >= 0; i--){
        seq1[i] += seq2[i];
    }
    index = 0;
    for (auto i : seq1){
        if (i != 0) index++;
    }
    cout << index;
    for (int i = max; i >= 0; i--){
        if (seq1[i] == 0) continue;
        printf(" %d %.1f",i,seq1[i]);
    }
    cout << endl;
    return 0;
}