PAT A1009 Product of Polynomials (25)

来源:互联网 发布:免费下载圈点软件 编辑:程序博客网 时间:2024/05/16 15:56

题目地址:https://www.patest.cn/contests/pat-a-practise/1009

题目描述:

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 aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.

输出格式(Output Specification):

For each test case you should output the product 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 up to 1 decimal place.

输入样例(Sample Input):

2 1 2.4 0 3.2
2 2 1.5 1 0.5

输出样例(Sample Output):

3 3 3.6 2 6.0 1 1.6


题意:

给出两个多项式的系数,求这两个多项式的乘积。


解题思路:

先获得第一个多项式的系数,然后再输入第二个系数时循环与第一个多项式的系数相乘,并将结果加到对应指数的系数上,最后得到要输出的所有非零系数的项。

注意点:

  • 答案的系数数组要至少开到 2001,因为两个最高幂次为 1000 的多项式相乘,最高幂次可以达到 2000
  • 没有必要开两个数组存放两个多项式,然后读完系数之后再处理,只需要第二个多项式的系数边读入边处理
  • 系数是从大到小输出。
  • 只需要输出非零系数的项。

C++完整代码如下:

这里写代码片
0 0
原创粉丝点击