Contents
  1. 1. Input Specification:
  2. 2. Output Specification:
  3. 3. Sample Input:
  4. 4. Sample Output:

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

题目大意:给出两行代表两个多项式,每一行第一个数字代表项数,之后每对数字的是未知数指数,后一个是系数,求出两个多项式相乘后的结果。题目给出例子的第一行代表2.4x+3.2;第二行代表1.5x2+0.5x,相乘后结果为3.6x3+6x2+1.6x。答案第一个数字是结果多项式的项数,其后每一项的系数保留一位小数。

分析:将两个多项式分别保存在两个double数组b[]和b[]中,将相乘后的结果保存到另一个数组c[]中。对结果c[]进行遍历,若某一项不为0则表示该项存在,下标是未知数指数,其中的数据是其系数。

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
#include<iostream>
using namespace std;
int main(){
double a[1010]={0},b[1010]={0},c[2010]={0},y;
int n,x,cnt=0;
cin >> n;
for (int i=0;i<n;i++){
cin >> x >> y;
a[x] = y;
}
cin >> n;
for (int i=0;i<n;i++){
cin >> x >> y;
b[x] = y;
}
for (int i=1000;i>=0;i--){
if (a[i] == 0) continue;
for (int j=1000;j>=0;j--){
if (b[j] == 0) continue;
else c[i+j] += a[i] * b[j];
}
}
for (int i=2000;i>=0;i--)
if (c[i] != 0) cnt++;
cout << cnt;
for (int i=2000;i>=0;i--)
if (c[i] != 0) printf(" %d %.1f",i,c[i]);
return 0;
}
Contents
  1. 1. Input Specification:
  2. 2. Output Specification:
  3. 3. Sample Input:
  4. 4. Sample Output: