题目
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:
where is the number of nonzero terms in the polynomial, and are the exponents and coefficients, respectively. It is given that
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:
1 | 2 1 2.4 0 3.2 |
Sample Output:
1 | 3 2 1.5 1 2.9 0 3.2 |
题解
思路
- 理解题意
- 题意是,给两个多项式让你相加
- 就是指数相同的要把系数相加
- 输入格式是,先给你项数,然后一个指数,一个系数,一个指数,一个系数……这样整两行
- 输出格式同样,指数按从大到小排列。
- 一开始想到用哈希表来做
- 但是因为结果要排序,怪麻烦的
- 算了,数组一把梭
代码
数据结构
- poly是一个数组
- 下标代表指数
- 值代表系数
算法
就20行代码,还有近一半是空行,我也不知道该怎么说算法了,一看代码就懂。
代码本体
1 | poly = [0 for _ in range(1001)] |