首页 > 其他 > 详细

A1009 Product of Polynomials多项式相乘

时间:2020-02-08 21:28:12      阅读:62      评论:0      收藏:0      [点我收藏+]

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:

N?1?? a?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????

where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (,) 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 }

 

A1009 Product of Polynomials多项式相乘

原文:https://www.cnblogs.com/PennyXia/p/12285081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!