PTA甲级——1009

1009 Product of Polynomials

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:

$$
\begin{flalign}
K\ N_1\ a_{N_1}\ N_2\ a_{N_2}\ N_2\ a_{N_2}\ … N_k\ a_{N_k}\\
\end{flalign}
$$

where K is the number of nonzero terms in the polynomial, N i and a N i (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N K<⋯<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:

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

Sample Output:

1
3 3 3.6 2 6.0 1 1.6

思路

​ 题目和1002的思路非常接近,没看过的可以先看看1002.

​ 大致题意:和1002的区别就是这道题目是给两个多项式然后相乘,1002是相加。

​ 思路:其实就是一个模拟相乘的过程,可以将第一个多项式的每一项和第二个多项式的每一项都相乘一下然后就ok了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
const int N = 2e3 + 10;
double st[N];
int a[N];
double b[N];
int count = 0;

int main()
{
int k1, k2;
cin >> k1;
for (int i = 1; i <= k1; i++) {
int x;
double y;
cin >> x >> y;
a[i] = x;
b[i] = y;
}
cin >> k2;
for (int i = 1; i <= k2; i++){
int x;
double y;
cin >> x >> y;
for (int j = 1; j <= k1; j++) {
int k = x + a[j];
st[k] += y * b[j];
}
}

for(int i = 0; i < N; i++){
if(st[i] != 0) count++;
}

cout << count;
for (int i = N - 1; i >= 0; i--) {
if (st[i] != 0) {
// cout << " " << i << " " << st[i];
printf(" %d %.1lf", i, st[i]);
}
}

cout << '\n';
return 0;
}