A1009 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:

N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) are the exponents and coefficients, respectively. It is given that 1, 0.

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

思路:

使用a[1005]保存多项式A,在输入多项式B的时候一边将结果存入c[2005],最后输出count和c[i]。

 

 1 #include <iostream>
 2 using namespace std;
 3 int main() {
 4     int k1, k2, e1, e2, count = 0;
 5     double c1, c2;
 6     double a[1005] = { 0.0 }, c[2005] = { 0.0 };
 7     cin >> k1;
 8     for (int i = 0; i < k1; i++) {
 9         cin >> e1 >> c1;
10         a[e1] = c1;
11     }
12     cin >> k2;
13     for (int i = 0; i < k2; i++) {
14         cin >> e2 >> c2;
15         for (int j = 0; j <= 1000; j++) {
16             if (a[j] != 0) {
17                 c[j + e2] += a[j] * c2;//会存在同类项
18                 //break;
19             }
20         }
21     }
22     for (int i = 0; i <= 2000; i++) {
23         if (c[i] != 0)count++;
24     }
25     printf("%d", count);
26     for (int i = 2000; i >= 0; i--) {
27         if (c[i] != 0)printf(" %d %.1lf",i, c[i]);
28     }
29     return 0;
30 }

 

posted @ 2020-02-08 20:55  PennyXia  阅读(166)  评论(0编辑  收藏  举报