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 N_{1} a_{N_{1}} N_{2} a_{N_{2}} \ldots N_{K} a_{N_{K}} $$
where K is the number of nonzero terms in the polynomial,$$ N_{i} \text { and } a_{N_{i}}(i=1,2, \cdots, K)$$ are the exponents and coefficients, respectively. It is given that :
$$ 1 \leq K \leq 10,0 \leq N_{K}<\cdots<N_{2}<N_{1} \leq 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

题目翻译:

这次,我们求两个多项式A和B的和

输入规范:

每个输入文件包含一个测试用例,每个用例包含两行,每行包含一个多项式信息:$$ K N_{1} a_{N_{1}} N_{2} a_{N_{2}} \ldots N_{K} a_{N_{K}} $$。其中K是多项式中的非零项。$$ N_{i} \text { and } a_{N_{i}}(i=1,2, \cdots, K)$$分别是指数和系数,它们的范围是:$$ 1 \leq K \leq 10,0 \leq N_{K}<\cdots<N_{2}<N_{1} \leq 1000 $$

输出规范:

对于每个测试案例,你应该使用和输入的相同格式在一行上输出A与B的和,注意每行末尾不能有多余空间,请精确到小数点后1位。

思路:

使用数组c[i]=j来表示指数i的系数为j,先将数组初始化为0,输出时先遍历一下数组中非0的数目,也就是多项式的数目。然后再遍历一遍输出结果,即数组中的i和j的值。

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>
using namespace std;
int main() {
//c[i] = j表示指数i的系数为j
float c[1001] = {0};
int m, n, t;
float num;
//输入第一行
cin>>m;
for (int i = 0; i < m; i++) {
cin>>t>>num;
c[t] += num;
}
cin>>endl;
//输入第二行
cin>>n;
for (int i = 0; i < n; i++) {
cin>>t>>num;
c[t] += num;
}
cin>>endl;
int cnt = 0;
for (int i = 0; i < 1001; i++) {
if (c[i] != 0) cnt++;
}
cout<<cnt;
for (int i = 1000; i >= 0; i--) {
if (c[i] != 0.0)
printf(" %d %.1f", i, c[i]);
}
return 0;
}