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 |
|