【Python】PAT甲級 A1009:Product of Polynomials
題目內(nèi)容
This time, you are supposed to find ×?where? and??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 (=1,2,?,) are the exponents and coefficients, respectively. It is given that 1?K?10,0? N_K < ...?< N_2 < N_1 ?10000?NK<?<N2<N1?1000..
Output Specification:
For each test case you should output the product of and ?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 up to 1 decimal place.
Sample Input:
Sample Output:
題目要點
本題 25 分,是比較簡單的模擬題。在輸入時,Python可以不用考慮給定的,直接用數(shù)組切片隔位取指數(shù)列表、系數(shù)列表,再用zip()
構(gòu)造字典。
這道題與前面文章提到的A1002問題類似,一些相似的解法、代碼在前文中有提到,這里不在贅述。

重點需要考慮的是如何將多項式的指數(shù)和對應(yīng)系數(shù)映射。C/C++可以用數(shù)組下標(biāo)作為指數(shù),數(shù)組浮點數(shù)值為對應(yīng)系數(shù)。這里Python使用字典比較方便,而且考慮到兩個多項式的指數(shù)項不一定相同,在求和時要指數(shù)項系數(shù)對應(yīng)相加,Python可以使用集合的求并集|
運算符實現(xiàn)。
注意,測試點6是非零系數(shù)項個數(shù),也就是為0的情況,這時候只輸出0
。如果有多余空格會出現(xiàn)“格式錯誤”。
AC 源代碼