题目

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  aN1  N2  aN2...  Nk  aNkK\;N_1\; a_{N_1} \;N_2\; a_{N_2} ... \;N_k\; a_{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,⋯,K) are the exponents and coefficients, respectively. It is given that 1K100NK<<N2<N11000.1≤K≤10,0≤N_K<⋯<N_2<N_1≤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:

1
2
2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

1
3 2 1.5 1 2.9 0 3.2

题解

思路

  • 理解题意
  • 题意是,给两个多项式让你相加
  • 就是指数相同的要把系数相加
  • 输入格式是,先给你项数,然后一个指数,一个系数,一个指数,一个系数……这样整两行
  • 输出格式同样,指数按从大到小排列。
  • 一开始想到用哈希表来做
  • 但是因为结果要排序,怪麻烦的
  • 算了,数组一把梭

代码

数据结构

  • poly是一个数组
    • 下标代表指数
    • 值代表系数

算法

就20行代码,还有近一半是空行,我也不知道该怎么说算法了,一看代码就懂。

代码本体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
poly = [0 for _ in range(1001)]

def add():
global poly
line = input().split()[1:]
i = 0
while i < len(line) - 1:
poly[int(line[i])] += float(line[i + 1])
i += 2

add()
add()

count = 0
for i in range(1000,-1,-1):
if poly[i] != 0:
count += 1

print(count, end='')
for i in range(1000, -1, -1):
if poly[i] != 0:
print(" %d %.1f" % (i, poly[i]), end='')